UIWebView Inside UIScrollView Content Size

I need to add a UIWebView and some buttons and labels inside a UIScrollView . I saw a lot of questions about how to add a UIWebView inside a UIScrollView , but unfortunately they could not help me complete my task. So kindly do not mark this as a duplicate or something like that.

Theoretically, the problem can be solved using the following steps:

1) Make UIWebView sub-item of UIScrollView .

2) Call setScrollEnabled:NO for the UIWebView to disable its own scrolling.

3) Set the content size of both UIScrollView and UIWebView for the size of the HTML string loaded inside the UIWebView .

I am using iOS 6 and StoryBoards. I added UIWebView as a sub-item of UIScrollView .

Then in my webViewDidFinishLoad I used the following:

  NSString *webHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.height;"]; NSLog(@"WebView Height %@", webHeight); 

This gives me the height of the webview.

In StackOverFlow, I came across the following.

  CGRect frame = webView.frame; frame.size.height = 1; webView.frame = frame; CGSize fittingSize = [webView sizeThatFits:CGSizeZero]; frame.size = fittingSize; webView.frame = frame; NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height); 

Then I set the size of the UIScrollView to the size of the UIWebView :

 mainScrollView.contentSize = webView.bounds.size; 

I can scroll down, but there is a lower space at the bottom, and the UIWebView does not make its size according to the size of the loaded html content.

enter image description here

It seems that UIScrollView resized it to the size of the contents of the UIWebView , but the UIWebView does not display it all. How can I solve this problem?

Please do not advise me about the Apple documentation that says you should not use UIWebView inside a UIScrollView . I already know this, but I want to use it in accordance with the requirements of my project.

+8
ios iphone xcode uiscrollview uiwebview
source share
3 answers

I think I can shed some light. I think you are confusing frame size and content size. First, I will describe the methodology, and then I will make concrete suggestions.

Methodology

You have a webView, and you have correctly disabled scrolling on it and set contentSize to the full size of the web content. You must also set the frame size to the full size of the web content. Then you add this view to scrollView at the beginning (0,0). Set scrollView contentSize to full size web content. Set the scrollView frame to the viewController.view frames.

Specific offers

  • The "fittingSize" code you posted does not suit me. The purpose of this code is to set the webView frame. Use NSLog or breakpoints to find out if this sets the full size of the web content.
  • Please note that the purpose of the javascript code you posted is the same as the "fitSize" code you entered. Check which one produces the correct conclusion.
  • Try to figure out the sizes of the webView and scrollView frames to help you debug. The best way to do this is to set different background colors as follows: setBackgroundColor:[UIColor redColor]
  • My best guess is that webView.frame.size not set large enough.
+2
source share

Ive found the same situation that webView does not reflect the change in frame size, the space remains until it scrolls down, as Jessica mentioned. I checked my project and found that Ive autostart function is enabled, so Ive solutions were found:

  • disable autorun.
  • add a height limit to the webView by resizing the WebView frame using the following code:

    self.webViewHeightConstraint.constant = fittingSize.height; [webView updateConstraints];

+2
source share

To get the height of the contents of a UIWebView , I highly recommend the "Phenomenon" answer here: https://stackoverflow.com/a/168268/ He / she suggests NOT using "CGSizeZero" and gives the best solution.

0
source share

All Articles