webView自适应内容高度(完美解决)

-(void)loadWebView

{

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURLStr]];

self.webView.scrollView.scrollEnabled = NO;

self.webView.scrollView.bounces = NO;

[self.webView sizeToFit];

[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

[self.webView loadRequest:request];

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context {

if ([keyPath isEqualToString:@"contentSize"]) {

CGSize actualSize = [_webView sizeThatFits:CGSizeZero];

CGRect newFrame = _webView.frame;

newFrame.size.height = actualSize.height;

_webView.frame = newFrame;

}

}

你可能感兴趣的:(webView自适应内容高度(完美解决))