【发布时间】:2014-05-28 20:08:55
【问题描述】:
我有一个详细视图,我想在其中显示文章的标题、副标题和内容。我希望能够使用 HTML 来格式化文本,所以我使用 UIWebView 来显示文章正文。这非常有效。
然而,所有这些都在 UIScrollView 中,所以我的问题是我必须计算 UIScrollView 的高度?
这就是今天的工作方式:
这就是它在 Storyboard 中的样子:
那么我需要弄清楚的是,计算 UIScrollView 正确高度的正确代码和语法是什么?在几件事中,我尝试了[self.scrollView sizeToFit],但没有运气。
编辑:显然它使用下面的代码设置了正确的高度,但似乎视图永远不会更新。
-(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);
}
控制台报告了明显正确的高度:
new frame: 582.000000, scrollview frame: 582.000000
在 Photoshop 中快速检查一下,这似乎是正确的:
绿色和蓝色区域的总和值为582像素,但滚动视图仍然只是将504像素区域从导航栏下方滚动到屏幕底部(到标签栏底部)。
【问题讨论】:
标签: ios uiwebview uiscrollview