xcode UIWebview 调整高度以适应内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16540050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIWebview resize height to fit content
提问by Recycled Steel
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.
我知道这已经被问过很多次了,但我无法让它发挥作用。简而言之,我的 UIWebview 引入了一些没有固定高度(宽度始终相同)的 HTML,我只希望高度相应地改变。
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.
我已经尝试了许多我在网上找到的修复程序,但我的问题是 webview 将更改为正确的大小,但只持续一秒钟,然后轻弹回到原始大小。
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.
我应该指出,我的这个观点是由 segue 调用的,这是我唯一能想到的不同之处!我想知道代码是否有效,但是当 segue 完成时,它会淡入淡出、翻转或其他任何方式,然后重新绘制 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.
这是我当前的代码(正如我所说,我已经尝试了许多修复程序,包括 Java 版本,其中大部分都是这样做的),我还使用了 scalesPageToFit 和 srollEnabled 以及 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
起始尺寸:176.000000 完成尺寸:246.000000
Anyone got an idea as to why the Webview appears fine just long enough for me to notice and then "shrink" back?
任何人都知道为什么 Webview 看起来很好,只要我注意到然后“缩小”回来?
采纳答案by Tommie C.
Anyone got an idea as to why the Webview appears fine just long enough for me to notice and then "shrink" back?
任何人都知道为什么 Webview 看起来很好,只要我注意到然后“缩小”回来?
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.
我会说它看起来很好,因为您正在显示正在发生的变化。为什么不在视图生命周期的某个较早的时间点甚至在 webViewDidFinishLoading 中隐藏 Web 视图?我正在从 IB 分享我的布局设置,以防万一可能会提供进一步的帮助。请告知这是否有帮助或是否需要其他信息。我使用的是弹簧和支柱而不是自动布局。我会尝试这些方法,看看它是否适合你。
Hiding my web view data loads prevents users from seeing anything until all of my settings/data are in place.
隐藏我的 web 视图数据加载可以防止用户看到任何东西,直到我的所有设置/数据都到位。
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];