Muhammad Saqib
Muhammad Saqib

Reputation: 991

iPhone - get the Content Size of UIWebView

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;
} 

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

Upvotes: 1

Views: 7967

Answers (2)

Niraj
Niraj

Reputation: 1964

The webViewDidFinishLoad method may be called more than once, and the first value returned by sizeThatFits is only some portion of what the final size should be. Then for whatever reason the next call to sizeThatFits when webViewDidFinishLoad fires again will incorrectly return the same value it did before! This will happen randomly for the same content as if it's some kind of concurrency problem. Maybe this behaviour has changed over time, because I'm building for iOS 5 and have also found that sizeToFit works in much the same way (although previously this didn't?)

I have settled on this simple solution:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{        
    CGFloat height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
    CGFloat width = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
    CGRect frame = aWebView.frame;
    frame.size.height = height;
    frame.size.width = width;
    aWebView.frame = frame;
}

Swift (2.2):

func webViewDidFinishLoad(webView: UIWebView) {

if let heightString = webView.stringByEvaluatingJavaScriptFromString("document.height"),
    widthString = webView.stringByEvaluatingJavaScriptFromString("document.width"),
    height = Float(heightString),
    width = Float(widthString) {

    var rect = webView.frame
    rect.size.height = CGFloat(height)
    rect.size.width = CGFloat(width)
    webView.frame = rect
}

}

Update: I have found as mentioned in the comments this doesn't seem to catch the case where the content has shrunk. Not sure if it's true for all content and OS version, give it a try.

Upvotes: 0

Lvsti
Lvsti

Reputation: 1525

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

1. The ugly way

Using JS to query the document properties:

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

2. The less ugly way

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;
    }
}

Upvotes: 6

Related Questions