美文网首页
关于UIWebView - 获取内容高度

关于UIWebView - 获取内容高度

作者: 居然是村长 | 来源:发表于2016-04-13 21:42 被阅读175次

使用 JS 代码获取内容高度

web 加载完成之后。注:由于图片的原因可能还是不一定准。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
}

KVO

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

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGSize size = [[change objectForKey:NSKeyValueChangeNewKey] CGSizeValue];
}

RAC

    [[self.webView.scrollView rac_valuesAndChangesForKeyPath:@"contentSize"
                                                 options:NSKeyValueObservingOptionNew
                                                observer:nil]
     subscribeNext:^(RACTuple *x) {
         @strongify_self
         CGSize size = [[[[x allObjects] lastObject] objectForKey:NSKeyValueChangeNewKey] CGSizeValue];
     }];

1

相关文章

网友评论

      本文标题:关于UIWebView - 获取内容高度

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