Implementing UIWebView inside UIScrollView

2019-09-02 05:49发布

问题:

I'm trying to use UIWebView inside UIScrollView, I want to display an webview under an image and make them scrollable. So I flowed those step :

  1. Desactivate the Scroll Property for UIWebView
  2. Set the UIScrollView and UIWebView height equals to content size.

And this my code :

- (void)viewDidLoad {
    [super viewDidLoad];

        self.contentReader.scrollView.scrollEnabled = NO;
        NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                       "<head> \n"
                                       "<style type=\"text/css\"> \n"
                                       "body {font-family: \"%@\"; font-size: %d;}\n"
                                       "</style> \n"
                                       "</head> \n"
                                       "<body>%@</body> \n"
                                       "</html>", @"JF Flat", 18, self.thePost.content];

        [self.contentReader loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:right; text-align:justify; direction:rtl'><style type='text/css'>img { max-width: 100%%; width: auto; height: auto; }</style><style type='text/css'>iframe { max-width: 100%%; width: auto; height: auto; }</style>%@<div>",myDescriptionHTML] baseURL:nil];

    }
    -(void)webViewDidFinishLoad:(UIWebView *)webView {
        // get the html content height
        NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.height;"];
        NSLog(@"Content Size %@",output);
        // adjust the height of webView
        NSLog(@"WebView Hieght before Update%f", webView.frame.size.height);
        CGRect frame = webView.frame;
        frame.size.height = 1;
        webView.frame = frame;
        CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
        frame.size = fittingSize;
        webView.frame = frame;
        NSLog(@"WebView Hieght After Update%f", webView.frame.size.height);
        [self.scroller setContentSize:webView.bounds.size];
    }

And This is the Log message :

[5150:203791] WebView Hieght before Update360.000000
[5150:203791] WebView Hieght After Update5888.000000

The Problem is that when I scroll down the content is not showed, nothing is showed. check the picture :

Update Debug View Hearachy.

As you can see the WebView height in the view hierarchy hasn't changed. Only the web page has changed the size.

回答1:

We already solved it in the comments, so let's just put this answer for completeness' sake.

If you're using Auto Layout, make sure to change the size of the WebView by setting constraints such as a height constraint and changing its constant to make the layout change. If you want to animate a specific Auto Layout change, do something like this:

[UIView animateWithDuration:0.5 animations:^{
    self.webViewHeightConstraint.constant = fittingSize.height;
    [self.view layoutIfNeeded];
}];


回答2:

Kind of a long shot but you might want to try

webView.scrollView.contentSize = fittingSize;

or

webView.scrollView.frame = CGRectMake(0,0,fittingSize.width, fittingSize.height);

Because scrolling was turned off the content size or frame may not be changing after loading the html. Like I said a long shot but might work.