iPhone - 获取UIWebView的内容大小 [英] iPhone - get the Content Size of UIWebView

查看:100
本文介绍了iPhone - 获取UIWebView的内容大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的UIWebView中打开本地Pdf文件而不是尝试调整UIWebview的大小,使得WebView高度等于WebView内容高度我使用这行代码来获取WebView内容的高度

I am opening the local Pdf File in my UIWebView and and than trying to adjust the size of UIWebview such that WebView height become equal to WebView Content height i am using this line of code to get the height of WebView Content

- (float) getWebViewContentHeight:(UIWebView *)view
{


    CGRect aFrame               = view.frame;

    aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
    view.frame              = aFrame;

    return view.frame.size.height;
} 

此代码在iOS 4.3上完美运行但在iOS 5.0上无法正常工作一个帮助我PLZ如何获取webView的内容大小或使此代码也适用于iOS 5

this code is working perfectly on iOS 4.3 but not working on iOS 5.0 can any one help me plz how to get the content size of webView or make this code working for iOS 5 as well

推荐答案

访问内容有点棘手,但在这里我将展示2种方法。

Accessing the content is a bit tricky but here I'll show 2 ways to do it.

1。丑陋的方式

使用JS查询文档属性:

Using JS to query the document properties:

CGSize contentSize = CGSizeMake([[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] floatValue],
                                [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue]);

2。不那么丑陋的方式

UIWebView 是一个复合对象,其中包含一个内部的UIScrollView 。从iOS 5.0开始,您可以尝试抓取此滚动视图并直接将其 contentSize 属性用作 webView.scrollView.contentSize 。如果你需要保持与iOS 4.3及之前的兼容性,你必须做一些黑魔法来获得滚动视图:

UIWebView is a composite object which contains an internal UIScrollView. Starting from iOS 5.0, you can try grabbing this scroll view and using its contentSize property directly as webView.scrollView.contentSize. In case you need to maintain compatibility with iOS 4.3 and before, you have to do some black magic to get the scroll view:

UIScrollView* webScrollView = nil;
for ( UIView* subview in [webView subviews] )
{
    if ( [subview isKindOfClass:[UIScrollView class]] )
    {
        webScrollView = (UIScrollView*)subview;
        break;
    }
}

这篇关于iPhone - 获取UIWebView的内容大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆