webView自适应内容高度

1.添加监听

//禁止webView内部的scrollView的滚动

self.webView.scrollView.scrollEnabled = NO;

//kvo监听webView内部的scrollView的contentSize

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

 

 

/**

 *  kvo监听webView.scrollView.contentSize的变化

 *

 */

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

{

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

        //把webView的高度设置成内容的总高度

        self.webView.height = self.webView.scrollView.contentSize.height;

        

        //通知  把webview最新的高度 通过block通知外面

        if (self.cellHeightChange) {

            self.cellHeightChange(kCellH + self.webView.height);

        }

        

    }

}

 

 

//3.删除监听

- (void)dealloc

{

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

}

posted @ 2017-05-09 15:40  湘岳  阅读(1906)  评论(0编辑  收藏  举报