在ios swift中使用AutoLayout根据html文本更改webview高度
创建了一个加载html文本的webview在ios swift中使用AutoLayout根据html文本更改webview高度,ios,swift,xcode,uiwebview,wkwebview,Ios,Swift,Xcode,Uiwebview,Wkwebview,创建了一个加载html文本的webview var html_string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scramb
var html_string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
self.webView.loadHTMLString(html_string, baseURL: nil)
我想将webview高度设置为其内容。如果不在webview中滚动,请记住组件是WKWebView,webview已成为过去 尝试此功能:
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.scrollView.isScrollEnabled=false;
myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height
webView.scalesPageToFit = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame.size.height = 1
webView.frame.size = webView.scrollView.contentSize
}
记住为myWebViewHeightConstraint和updateConstrain创建一个出口
祝你今天愉快 使用下面的代码,希望对您有所帮助 注意:-“UIWebView”在iOS 12.0中已被弃用:不再受支持,因此请使用WKWebView而不是UIWebView 对于UIWebView 对于WKWebView 如果要使用WKWebView,则需要执行以下操作 1导入WebKit 2使您的ViewController从WKNavigationDelegate继承 3连接WKWebView的委托:webView.navigationDelegate=self 4实现以下协议功能:
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.scrollView.isScrollEnabled=false;
myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height
webView.scalesPageToFit = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.frame.size.height = 1
webView.frame.size = webView.scrollView.contentSize
}
可能重复感谢您的答复,但我没有得到完整的内容高度,我已经提到在故事板webView高度200。如果内容高度超过200,则不会增加高度,而是提供可滚动的视图hi@nshivaprasadeddy我认为您已从情节提要设置了固定高度200,您需要更新此设置,将高度设置为0或更大,然后约束高度>=0另一个选项不通过自动调整大小来指定约束