美文网首页闻道丶iOS(尝鲜版)
tabViewCell里面添加webView

tabViewCell里面添加webView

作者: xcp123 | 来源:发表于2017-03-23 09:29 被阅读0次

在UItabViewCell里面添加UIWebView时需要注意在以下方法中的用法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return self.webView.height;

}

-(void)webViewDidFinishLoad:(UIWebView *)webView

{

//获取到webview的高度

//    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];

CGFloat heights = [self.webView sizeThatFits:CGSizeZero].height;

self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, SWIDTH, height);

[self.webView sizeToFit];

[_tableView reloadData];

[webView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

[webView setHeight:webView.scrollView.contentSize.height];

[webView.scrollView setScrollEnabled:NO];

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];

}

//添加事件监听

- (void)addObservers

{

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

}

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

{

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

[self scrollViewContentSizeDidChange:change];

}

}

- (void)dealloc

{

//移除

[self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];

}

- (void)scrollViewContentSizeDidChange:(NSDictionary *)change

{

[self.webView setHeight:self.webView.scrollView.contentSize.height];

[_tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];

}

因为webView没有contentSize的属性,所以要用self.webView.scrollView

自己的一点小总结。希望也能帮到像我一样困惑的朋友

相关文章

网友评论

    本文标题: tabViewCell里面添加webView

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