EventWebView.frame.size = EventWebView.sizeThatFits(CGSize.zero)
EventWebView.scrollView.isScrollEnabled = false;
var frame = EventWebView.frame;
frame.size.width = EventTableView.frame.size.width-40; // Your desired width here.
frame.size.height = 1; // Set the height to a small one.
EventWebView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).
frame.size.height = EventWebView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView.
heightConstraint.constant = frame.size.height
我的HTML内容“
测试由Tester创建的新事件的描述。测试Tester.Testing对新创建的事件的描述由Tester创建的新事件的描述。测试由Tester测试的新创建事件的描述。测试Tester.Testing对新创建事件的描述Tester.Testing对新创建事件的描述Tester对新创建事件的描述。测试Tester.Testing对新创建事件的描述Tester.Testing对新创建事件的描述Tester对新创建事件的描述。测试Tester.Testing对新创建事件的描述Tester.Testing对新创建事件的描述Tester对新创建事件的描述。测试Tester.Testing对新创建事件的描述Tester.Testing对新创建事件的描述Tester对新创建事件的描述。测试Tester.Testing对新创建的事件的描述由Tester描述新创建的事件。
\ n“
答案 0 :(得分:4)
添加WebViewDelegate
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size = webView.sizeThatFits(CGSize.zero)
webView.scrollView.isScrollEnabled = false; // Property available in iOS 5.0 and later
var frame = webView.frame;
frame.size.width = self.view.frame.width-20; // Your desired width here.
frame.size.height = 1; // Set the height to a small one.
webView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above).
frame.size.height = webView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView.
webView.frame = frame;
fl = webView.frame.size.height
}
请务必检查自动布局约束。这种方法工作正常。如果您的自动布局设置不正确,那么您可以获得额外的长度。
答案 1 :(得分:0)
试试这个
NSString * contentHeight = [webView stringByEvaluatingJavaScriptFromString:@“document.body.offsetHeight;”];
int height = [contentHeight integerValue];
webViewDidFinishLoad方法中的。确保为Web视图设置委托,并在视图控制器中实现上述委托方法。