Iphone – UIWebView won’t resize to fit content

iphoneuiweb

My webView is not resizing properly after it finished loading. I have tried this code.

- (void)webViewDidFinishLoad:(UIWebView *)webview {
    CGRect frame = webView.frame;
    frame.size.height = 1;
    frame.size.width = screenWidth;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    [bodyScroll setContentSize:CGSizeMake(screenWidth, webView.frame.origin.y + webView.frame.size.height+keyboardRectY+60)];

It is incresing the width beyond the screenWidth and hence my content horizontally expands beyond the view size which I don't want.

changing

CGSize fittingSize = [webView sizeThatFits:CGSizeMake(screenWidht, 1)];

also leads to the same situation.

How can I get rid of this? The webview width should always remain as screenwidth and only it's height should adjust based on the content size so that the webView just about fits the whole content.

Thanks in Advance

Best Answer

When setting the size of frame, you are overwriting the width you set previously. This should it:

...
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
fittingSize.width = screenWidth; // Add this
frame.size = fittingSize
...
Related Topic