在UIScrollView中实现UIWebView [英] Implementing UIWebView inside UIScrollView

查看:62
本文介绍了在UIScrollView中实现UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UIScrollView中使用UIWebView,我想在图像下显示一个Webview并使它们可滚动.因此,我执行了这些步骤:

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. 停用UIWebView的Scroll属性
  2. 设置UIScrollView和UIWebView的高度等于内容大小.

这是我的代码:

- (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];
    }

这是日志消息:

[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 :

更新调试视图Hearachy.

Update Debug View Hearachy.

您可以看到视图层次结构中的WebView高度没有改变.只有网页更改了大小.

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

推荐答案

我们已经在注释中解决了这个问题,因此为了完整起见,我们只给出这个答案.

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

如果您使用的是自动布局,请确保通过设置约束(例如高度约束)并更改其常量以更改布局来更改WebView的大小.如果要设置自动版式更改的动画,请执行以下操作:

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];
}];

这篇关于在UIScrollView中实现UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆