在 UIScrollView 中实现 UIWebView
Implementing UIWebView inside UIScrollView
我正在尝试在 UIScrollView 中使用 UIWebView,我想在图像下显示一个 webview 并使它们可滚动。
所以我完成了这些步骤:
- 为 UIWebView
停用滚动 属性
- 设置 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
问题是当我向下滚动时没有显示内容,什么也没有显示。查看图片:
更新
调试视图 Hearachy。
如您所见,视图层次结构中的 WebView 高度没有改变。只有网页改变了尺寸。
希望不大,但您可能想试试
webView.scrollView.contentSize = fittingSize;
或
webView.scrollView.frame = CGRectMake(0,0,fittingSize.width, fittingSize.height);
由于滚动已关闭,内容大小或框架在加载 html 后可能不会改变。就像我说的远射,但可能会奏效。
我们已经在评论中解决了,所以为了完整起见,让我们把这个答案放在这里。
如果您使用的是自动布局,请确保通过设置高度约束等约束来更改 WebView 的大小,并更改其常量以更改布局。如果您想为特定的自动布局更改设置动画,请执行以下操作:
[UIView animateWithDuration:0.5 animations:^{
self.webViewHeightConstraint.constant = fittingSize.height;
[self.view layoutIfNeeded];
}];
我正在尝试在 UIScrollView 中使用 UIWebView,我想在图像下显示一个 webview 并使它们可滚动。 所以我完成了这些步骤:
- 为 UIWebView 停用滚动 属性
- 设置 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
问题是当我向下滚动时没有显示内容,什么也没有显示。查看图片:
更新
调试视图 Hearachy。
如您所见,视图层次结构中的 WebView 高度没有改变。只有网页改变了尺寸。
希望不大,但您可能想试试
webView.scrollView.contentSize = fittingSize;
或
webView.scrollView.frame = CGRectMake(0,0,fittingSize.width, fittingSize.height);
由于滚动已关闭,内容大小或框架在加载 html 后可能不会改变。就像我说的远射,但可能会奏效。
我们已经在评论中解决了,所以为了完整起见,让我们把这个答案放在这里。
如果您使用的是自动布局,请确保通过设置高度约束等约束来更改 WebView 的大小,并更改其常量以更改布局。如果您想为特定的自动布局更改设置动画,请执行以下操作:
[UIView animateWithDuration:0.5 animations:^{
self.webViewHeightConstraint.constant = fittingSize.height;
[self.view layoutIfNeeded];
}];