I had a requirement which was scroll the entire view, it contains the customView and WebView. The struct like below image:
Therefore, I choose scrollView as the superView to make it scroll. I use the snapkit to layout all views. I added a containView to scrollView which had the customView and the webView. The code like below:
func setupScrollView() {
view.addSubview(scrollView)
scrollView.snp.makeConstraints { (make) in
make.edges.equalTo(view)
}
scrollView.addSubview(containView)
containView.snp.makeConstraints { (make) in
make.left.right.equalTo(view)
make.top.bottom.equalTo(scrollView)
}
}
func setupHeaderView() {
containView.addSubview(headerView)
headerView.snp.makeConstraints { (make) in
make.left.right.equalTo(view)
make.top.equalTo(containView)
}
}
func setupWebView() {
containView.addSubview(webView)
webView.navigationDelegate = self
webView.scrollView.bounces = false
webView.scrollView.isScrollEnabled = false
loadLocalHtml()
}
I had read some documents about how to get the webView's height , it is wait the webView load finished, then set the height of the webView.It can make the scrollView work. So I want to follow this ideas, but I had different situations:
1) I need to load the local html , when webView load finished. I need to make a request to get data form the server.
2) Then I called js method of the webView to fill the data to html. The html contains image and text.
I have think about how to do it. So I get the height of the webView when call js method success. The code below:
func updateWebView(_ json: String) {
let userId = userManager.currentUserId()
webView.evaluateJavaScript("updateData(\(json),'\(userId)')") { [weak self](response, error) in
guard let strongself = self else { return }
if error == nil {
strongself.settingconstraints()
}
}
}
func settingconstraints() {
webView.snp.updateConstraints { make in
make.left.right.equalTo(view)
make.top.equalTo(headerView.snp.bottom)
make.height.equalTo(webView.scrollView.contentSize.height)
make.bottom.equalTo(containView)
}
}
But it couldn't scroll, I don't know how to solve it now? Please help. Thanks
