2

I did this exact same thing with a XIB and it works, but something is wrong with my storyboard version.

Here's my UIWebView Delegate code

#pragma mark - UIWebViewDelegate Protocol Methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {

    CGRect frame = webView.frame;
    frame.size.height = webView.scrollView.contentSize.height;
    _webView.frame = frame;

    NSLog(@"Webview Frame height is %f", _webView.frame.size.height);

    _scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, _headerImageView.frame.size.height + webView.frame.size.height);

    NSLog(@"scrollview content height is %f", _scrollView.contentSize.height);

}

NSLog is showing proper webview frame height of about 800 points. Scrollview content size is perfect about 1000 points.

My Scrollview is set to have a red background. So you can see here the webview frame is only about 500 points on screen and the rest of the content is getting cut off.

Any ideas? Thanks!

enter image description here

sayguh
  • 2,540
  • 4
  • 27
  • 33
  • Full details on how I solved this can be found here: http://stackoverflow.com/questions/26699565/autolayout-constraint-that-uses-uiscrollview-contentsize – sayguh Nov 04 '14 at 15:34

1 Answers1

2

The problem is that auto layout is responsible for both the frame of the web view and the content size of the scroll view. You can set them, and of course if you then read them, you read what you just set; but you're just misleading yourself, because then (outside your method) auto layout comes along and sets them for real, and what you are seeing (as opposed to your meaningless NSLog readings) is the result of auto layout's settings. You need to configure your constraints properly so that auto layout does what you want it to do.

Auto layout wasn't turned on in the .xib file, so you didn't encounter this behavior. But it's turned on in your storyboard.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you very much for this explanation. But do you know how I can set my constraints properly when dealing with variable content size of my UIWebView? Doesn't that need be determined in the delegate didfinishload? – sayguh Nov 02 '14 at 02:20
  • I am unclear on what you are trying to do. The web view has an internal scroll view and it will set that scroll view's content size so that the user can view the whole web page. You don't have to do anything. Now, you've also got _another_ scroll view; I don't know why (a scroll view inside a web view inside a scroll view????). Anyway, you can set the outer scroll view's `contentSize`, but you must do it with _constraints_ of things inside it, or you must turn off auto layout on the interior of the scroll view, as I explain here: http://stackoverflow.com/a/13548039/341994 – matt Nov 02 '14 at 02:47
  • I have one scollview with a webview and other views inside it that I want to all scroll together. I have disabled the scrollview that comes as part of the webview. I'll read your link! – sayguh Nov 02 '14 at 03:12
  • Hey Matt, how can I add a height constraint to my UIWebView that is pinned to contentsize of the scrollview superview? – sayguh Nov 02 '14 at 12:54