gpt4 book ai didi

ios - 在 UIScrollView 中实现 UIWebView

转载 作者:行者123 更新时间:2023-11-28 19:52:24 25 4
gpt4 key购买 nike

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

  1. 停用 UIWebView 的滚动属性
  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

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

更新调试 View Hearachy。 enter image description here

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

最佳答案

我们已经在评论中解决了它,所以为了完整起见,我们只给出这个答案。

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

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

关于ios - 在 UIScrollView 中实现 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com