美文网首页
WKWebView加载HTML字符串问题

WKWebView加载HTML字符串问题

作者: 每日总结 | 来源:发表于2018-09-06 17:46 被阅读430次

根据HTML的contentSize自适应WKWebView的高度

static CGFloat oldHeight=0;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    if ([object isEqual:self.webView.scrollView]&&[keyPath isEqualToString:@"contentSize"]) {
        NSLog(@"height=================%@",change[NSKeyValueChangeNewKey]);
        CGFloat height = self.webView.scrollView.contentSize.height;
        if (oldHeight != height) {
        oldHeight = height;
            if (height<=self.webView.frame.size.height) {
                [self.webView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(height/2);
                }];
            } else {
                [self.webView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(height);
                }];
            }
            
        }
    }
}

相关文章

网友评论

      本文标题:WKWebView加载HTML字符串问题

      本文链接:https://www.haomeiwen.com/subject/kiaxgftx.html