问题描述
考虑到在Web View之后必须包含其他元素,如何将WKWebView放入Scroll View.
How can I put a WKWebView inside a Scroll View consider that I must have others elements after Web View.
. View... Scroll View....... ImageView....... WKWebView....... Button
. View... Scroll View....... ImageView....... WKWebView....... Button
- 问题1:禁用WKWebView滚动
- 问题2:设置WKWebView的完整内容高度
检查我的示例项目
推荐答案
如果要获取WKWebView
中html的内容高度以使其在UIScrollView
中不可滚动,请继续阅读.
If you mean to get the content height of the html in WKWebView
to make it non-scrollable inside UIScrollView
, then read on.
您需要(通过委托)观察webView的内容高度.我正在为您提供这些步骤,因为您完全知道自己在做什么,并且快要到了.
You need to observe (via delegate) the content height of your webView. I'm giving you these steps, since you quite know what you're doing, and you're almost there.
摆脱网络视图高度限制的影响
@IBOutlet weak var constraint_WebViewHeight: NSLayoutConstraint!
订阅webView的代表
这会让您知道(您的控制器在您的webView加载完网站内容之后.
This will let you know (your controller when your webView has finished loading the contents of your site.
self.webView.navigationDelegate = self
符合协议
在WKNavigationDelegate
的didFinish navigation
方法中,然后调整webView高度约束的常数.如果需要,可以放一些动画:)
Inside the didFinish navigation
method of WKNavigationDelegate
, you then adjust the constant of your webView height constraint. Put some animation if you want to :)
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.constraint_WebViewHeight.constant = webView.scrollView.contentSize.height
}
}
}
最后,解决您的约束.
a.删除您的imageView底部约束.b.将底部约束添加到"scrollView"的保存"按钮中.
a. Remove your imageView bottom constraint.b. Add bottom constraint to your save button to the scrollView.
这将使您的scrollView可滚动.
This will make your scrollView scrollable.
Voila!这样就产生了结果.
Voila! This yields this result.
我希望这会有所帮助!
这篇关于滚动视图中的WKWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!