6

I am absolutely unable to resize my UIWebView.

My UIWebView is declared in .h and in connected in IB. The height in IB is 110. But the height of my text is about 1500. Now I'd like to change the height so the full text is readable without scrolling.

What code does resize my UIWebView in webViewDidFinishLoad? Can't find ANYTHING that works.

Venk
  • 5,949
  • 9
  • 41
  • 52
filou
  • 1,609
  • 5
  • 26
  • 53

3 Answers3

11

The UIScrollView property is available for UIWebView starting in iOS 5. You can use that to resize your view by implementing the webViewDidFinishLoad: message in your delegate, like this:

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect newBounds = webView.bounds;
    newBounds.size.height = webView.scrollView.contentSize.height;
    webView.bounds = newBounds;
}
Venk
  • 5,949
  • 9
  • 41
  • 52
John Stephen
  • 7,625
  • 2
  • 31
  • 45
  • 4
    There's an scenario when this won't work. When the UIWebview initially is higher than the new content size it will return the previous heigh: Eg. you create a webview with a height of 100px and then load an html with only one line of text that should use for example 15px, the content size on webViewDidFinishLoad is going to be 100px not 15px. But if your webview's height was initially 1px, on webViewDidFinishLoad the content size will return 15px. So to prevent this, make your webview always smaller than the expected height. – pablobart Sep 11 '14 at 19:52
  • @pablobart i have scenario where the scrollview height would be smaller after option changed / orientation changed. any fix for this? – neobie Apr 21 '16 at 12:45
3

Have you tried: this answer?

[Edit]

Some guys say this is working:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

// Change the height dynamically of the UIWebView to match the html content
CGRect webViewFrame = webView.frame;
webViewFrame.size.height = 1;
webView.frame = webViewFrame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
// webViewFrame.size.width = 276; Making sure that the webView doesn't get wider than 276 px
webView.frame = webViewFrame;

float webViewHeight = webView.frame.size.height;
}

This is untested by me but 70 people voted ^ to this answer and its recommend to set sizeTahtFits:CGSizeZero like a reset of your frame.size

and some people use JavaScript to fix this:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
CGFloat height = [string floatValue] + 8;
CGRect frame = [_webView frame];
frame.size.height = height;
[_webView setFrame:frame];

if ([[self delegate] respondsToSelector:@selector(webViewController:webViewDidResize:)])
{
    [[self delegate] webViewController:self webViewDidResize:frame.size];
}
}
Community
  • 1
  • 1
zero3nna
  • 2,770
  • 30
  • 28
  • thank you kalli, but the content should be dynamically implemented. the height may change. I know it is possible without size down the font. – filou Dec 13 '12 at 11:16
  • Technically the sizeThatFits: call should be passed `CGSizeMake(maxWidth, CGFLOAT_MAX)`. – Mike Weller Dec 13 '12 at 11:28
  • I built the UIWebView in IB (no subview). for now I set `self.webview.delegate = (id)self;` in viewDidLoad and `[self.webview loadHTMLString:description baseURL:nil];` - nothing else. If I add something like `self.webview = [[UIWebView alloc] init];` the UIWebView disappears. – filou Dec 13 '12 at 12:45
  • have you used `UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];` to init the webView? – zero3nna Dec 16 '12 at 15:16
2

as answered by john and eric above, this piece of code works.

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect newBounds = webView.bounds;
    newBounds.size.height = webView.scrollView.contentSize.height;
    webView.bounds = newBounds;
}

however while trying with this code i found out it returns me with a proper webview but improper origin, if thats the case for anyone, simply reset with this code

webView.frame=CGRectMake(webView.frame.origin.x, webView.center.y, webView.frame.size.width, webView.frame.size.height);

The y-axis is set and you get the webView as you wanted.

Maniac One
  • 589
  • 4
  • 23