【发布时间】:2015-04-12 11:10:40
【问题描述】:
我正在尝试在 UIScrollView 中使用 UIWebView,我想在图像下显示一个 webview 并使它们可滚动。 所以我流了那些步骤:
- 停用 UIWebView 的 Scroll 属性
- 设置 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