“UIWebView 自适应实际内容高度”

2016-03-22  本文已影响248人  橘花小茶

起因

公司提出要更改部分界面。其中一个界面是嵌入了一个webView的。要求整个页面都可以上下滚动,且一个UITextView放在UIWebView下面。

难点

分析

结果

dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
 });

这里的webViewHeight 实际使用时出现了无法完全展示网页的情况,最终在这个值的基础上加上100,解决!

 JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    context[@"htmlHeightChangeEvent"] = ^{
        //上述获取高度的方法
        dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
 });
    }];

不同步到主线程,会在网页加载完成后,改变网页高度时,报‘警告’,大意是说有可能出现诡异的错误。


The End

上一篇 下一篇

猜你喜欢

热点阅读