UIWebview resize height to fit content
I know this has been asked many times but I can not get it to work. In short my UIWebview pulls in some HTML which has a none fixed height (width always the same), I just want the height to change accordingly.
I have tried a number of fixes I have found online but my problem is the webview will change to the correct size but only for a brief second and then in flicks back to the original size.
I should point out that my this view is invoked by a segue, that is the only thing I can think is different! I am wondering if the code works but when the segue finishes it's crossfade, flip or whatever it then redraws the webview.
Here is my current code (as I say I have tried many fixes including the Java versions most of which do the same), I have also played with scalesPageToFit and srollEnabled plus a few of the ticks in the Storyboard.
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"Starting Size : %f", headerWebView.frame.size.height);
headerWebView.scalesPageToFit = YES;
headerWebView.scrollView.scrollEnabled = NO;
CGRect frame = headerWebView.frame;
frame.size.width = frame.size.width; // I used this to test, I put 200 in and it changes then flicks back too
frame.size.height = 1;
headerWebView.frame = frame;
frame.size.height = headerWebView.scrollView.contentSize.height;
headerWebView.frame = frame;
NSLog(@"Finished Size : %f", headerWebView.frame.size.height);
}
The logs say;
Starting Size : 176.000000 Finished Size : 246.000000
Anyone got an idea as to why the Webview appears fine just long enough for me to notice and then "shrink" back?
Anyone got an idea as to why the Webview appears fine just long enough for me to notice and then "shrink" back?
I would say that it appears fine because you are showing the changes as they are happening. Why not hide the web view at some earlier point in the view lifecycle or even in webViewDidFinishLoading? I am sharing my layout settings from IB in case that may provide further assistance. Please advise if this helps or whether additional information is needed. I am using springs and struts rather than auto layout. I would try these method to see if it works for you.
Hiding my web view data loads prevents users from seeing anything until all of my settings/data are in place.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView setAlpha:0];
[webView loadData:[dataString dataUsingEncoding:NSUTF8StringEncoding] MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL];
[UIView animateWithDuration:0.1 animations:^{
[webView setAlpha:1.0];
} completion:nil];
链接地址: http://www.djcxy.com/p/13550.html
上一篇: 如何在JavaScript中将数字格式设置为美元货币字符串?
下一篇: UIWebview调整高度以适应内容