WKWebView加载HTML字符串问题

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

static CGFloat oldHeight=0;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)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字符串问题)