Monday, February 22, 2010

UIWebView sizeToFit broken?

UIWebView sizeToFit does not work unless the webView starts with a non zero frame.
In the following code the webView will not be sized to fit. To correct the issue choose a non-zero size for the webview frame.
myWebView = [[UIWebView alloc] initWithFrame:CGRectZero] // wrong
[myWebView loadHTMLString:@"...lots of html here resulting in a very large webview..." baseURL:nil];

- (void)webViewDidFinishLoad:(UIWebView *)webView {
// if webview frame is zero size to fit will not work.
[webView sizeToFit];
NSLog(@"webView height: %f", webView.frame.size.height);
}

To correct the issue change: myWebView = [[UIWebView alloc] initWithFrame:CGRectZero] // wrong to myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,100)] // right

0 comments:

Post a Comment