【问题标题】:Implementing UIWebView inside UIScrollView在 UIScrollView 中实现 UIWebView
【发布时间】:2015-04-12 11:10:40
【问题描述】:

我正在尝试在 UIScrollView 中使用 UIWebView,我想在图像下显示一个 webview 并使它们可滚动。 所以我流了那些步骤:

  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

问题是当我向下滚动内容时没有显示,什么也没有显示。检查图片:

更新 调试视图 Heachy。

如您所见,视图层次结构中的 WebView 高度没有改变。只有网页改变了大小。

【问题讨论】:

  • 您应该调试视图层次结构以查看是否正确调整了 webView 和 scrollView 框架。日志消息似乎正确。
  • @OrtwinGentz 请检查我的更新
  • 只是一个想法,但您是否考虑过用户体验?因为实际上你最终会在滚动视图中得到一个滚动视图 (UIWebView),当用户滚动错误的视图时,这有时真的很烦人。
  • @Popeye 我已禁用 webview 滚动属性以避免这种混淆。此外,我需要显示带有一些文本的图像,并在其下显示 HTML 文本,这就是我在滚动视图中使用 imageview 和 uiwebview 的原因。还有任何建议或解决方案来实现我需要显示的内容吗?
  • 确实,这就是问题所在。您是否正在使用自动布局?然后你不能手动设置框架,但必须改变高度约束的常数。

标签: ios iphone uiwebview uiscrollview uikit


【解决方案1】:

我们已经在 cmets 中解决了它,所以为了完整起见,让我们把这个答案放上去。

如果您使用自动布局,请确保通过设置高度约束等约束来更改 WebView 的大小,并更改其常量以更改布局。如果您想为特定的自动布局更改设置动画,请执行以下操作:

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

【讨论】:

  • 谢谢,我明天用我的手机我会检查动画并给你我的反馈,如果你不介意的话我会在你的帖子上添加我的功能代码?
  • 好的,请随意添加。
  • 您好,您说我们需要添加高度约束。我们需要以编程方式添加它还是使用故事板?您在答案中发布的代码会在 webviewDidFinishLoad 函数中吗?
  • layoutIfNeeded 是什么意思。你能帮我解决这个问题吗?
  • @YestayMuratov 使用自动布局时,-layoutIfNeeded 会根据约束更改启动实际的布局更改。如果需要动画更改,则需要这样做。如果不需要动画,UIKit 会在下一次 runloop 迭代中自动改变布局,即使没有-layoutIfNeeded。
【解决方案2】:

有点远射,但你可能想尝试

webView.scrollView.contentSize = fittingSize;

或

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

由于滚动已关闭,加载 html 后内容大小或框架可能不会改变。就像我说的远射,但可能会奏效。

【讨论】:

  • 我试过这个,但总是遇到同样的问题,WebView 内容没有正确显示。
猜你喜欢
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 2013-09-16
  • 1970-01-01
  • 2015-04-13
  • 2023-03-31
相关资源
最近更新 更多