UIWebview调整高度以适应内容 [英] UIWebview resize height to fit content

查看:203
本文介绍了UIWebview调整高度以适应内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已被问过多次,但我无法让它发挥作用。简而言之,我的UIWebview提取了一些没有固定高度(宽度总是相同)的HTML,我只想相应地改变高度。

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.

我试过一个数字我在网上找到了一些修复但是我的问题是webview会改变到正确的大小,但只是短暂的一秒,然后再回到原来的大小。

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应该指出我的这个观点是由一个segue调用的,这是我能想到的唯一不同的东西!我想知道代码是否有效但是当segue完成它的交叉淡入淡出,翻转或其他什么它然后重绘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.

这是我当前的代码(正如我所说的,我有尝试了许多修复程序,包括Java版本,其中大多数都做同样的事情),我还使用了scalesPageToFit和srollEnabled以及故事板中的一些刻度。

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);
}

日志说;

起始尺寸:176.000000
成品尺寸:246.000000

Starting Size : 176.000000 Finished Size : 246.000000

任何人都知道为什么Webview看起来很好,只需要足够长的时间让我注意然后收缩回来?

Anyone got an idea as to why the Webview appears fine just long enough for me to notice and then "shrink" back?

推荐答案


任何人都知道为什么Webview看起来很好足够的
让我注意然后收缩回来?

Anyone got an idea as to why the Webview appears fine just long enough for me to notice and then "shrink" back?

我会说这看起来不错,因为你正在展示他们正在发生的变化。为什么不在视图生命周期的某个早期点甚至在webViewDidFinishLoading中隐藏Web视图?我正在从IB分享我的布局设置,以防可能提供进一步的帮助。请告知是否有帮助或是否需要其他信息。我使用弹簧和支柱而不是自动布局。我会尝试这些方法,看看它是否适合你。

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];


这篇关于UIWebview调整高度以适应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆