使用 UIWebView 确定 UIScrollView 的高度 [英] Deciding height for UIScrollView with UIWebView inside

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

问题描述

我有一个详细视图,我想在其中显示文章的标题、副标题和内容.我希望能够使用 HTML 来格式化文本,因此我使用了 UIWebView 来显示文章正文.这非常有效.

I have a detail view where I want to show a title, subtitle and content for articles. I want to be able to use HTML to format the text, so I've used a UIWebView for showing the article body. This works perfectly.

然而,所有这些都在 UIScrollView 中,所以我的问题是我必须计算 UIScrollView 的高度?

How ever, all of this, is inside a UIScrollView, so my issue is that I have to calculate the height of the UIScrollView?

这就是今天的工作方式:

This is how it works today:

这就是 Storyboard 中的样子:

And this is how it looks like in Storyboard:

那么我需要了解的是,计算 UIScrollView 正确高度的正确代码和语法是什么?在几件事中,我尝试了 [self.scrollView sizeToFit] 没有运气.

So what I need to find out, is what is the correct code and syntax to calculate the correct height of the UIScrollView? Amongst several things, I tried [self.scrollView sizeToFit] without luck.

显然它使用下面的代码设置了正确的高度,但似乎视图永远不会更新.

Apparently it sets the correct heights with the code below, but seems like the view never updates.

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    // get height of content in webview
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];

    // set new frame height
    CGRect frame = webView.frame;
    frame.size.height = height;
    webView.frame = frame; // webview is now correct height

    // set new frame height for scrollview (parent of webview)
    CGRect scrollFrame = self.scrollView.frame;
    scrollFrame.size.height = webView.frame.origin.y + height;
    self.scrollView.frame = scrollFrame;

    // log to console for cross checking
    NSLog(@"new frame: %f, scrollview frame: %f", scrollFrame.size.height, self.scrollView.frame.size.height);
}

控制台报告明显正确的高度:

The console reports the apparently correct height:

新框架:582.000000,scrollview框架:582.000000

在 Photoshop 中快速检查一下,这似乎是正确的:

And a quick check in Photoshop as well, this seems to be correct:

绿色和蓝色区域的总和为 582 像素,但是 scrollview 仍然只是将 504 像素区域从导航栏下方滚动到屏幕底部(到标签栏底部).

The summed value of green and blue area is 582 pixels, but the scrollview still just scrolls the 504 pixel area from below the navigation bar to the bottom of the screen (to the bottom of the tab bar).

推荐答案

我解决了这个问题.

首先,只需将 UIWebView 扩展到比内容更高的高度(例如 2000 像素).

First of all, just expand the UIWebView to a height higher than the content ever will be (e.g. 2000 pixels).

让奇迹发生的委托方法代码

The delegate method code that makes the magic happen

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    // set height for webiew
    webView.autoresizesSubviews = NO;
    webView.translatesAutoresizingMaskIntoConstraints = YES;
    webView.scrollView.autoresizesSubviews = NO;
    webView.scrollView.translatesAutoresizingMaskIntoConstraints = YES;

    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').clientHeight;"] floatValue] + 80; // +80 for tabbar and spacing
    CGRect frame = webView.frame;
    frame.size.height = height;
    webView.frame = frame;

    // fix height of scroll view as well
    self.scrollView.translatesAutoresizingMaskIntoConstraints = YES;
    self.scrollView.contentSize = CGSizeMake(320, (self.webView.frame.origin.y + self.webView.frame.size.height));
}

这篇关于使用 UIWebView 确定 UIScrollView 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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