WKWebView inside Scroll View

前端 未结 3 1336
遥遥无期
遥遥无期 2021-02-10 16:53

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

3条回答
  •  梦谈多话
    2021-02-10 17:47

    If you mean to get the content height of the html in WKWebView to make it non-scrollable inside UIScrollView, then read on.

    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.

    Make an outlet out of your webView height constraint

    @IBOutlet weak var constraint_WebViewHeight: NSLayoutConstraint!
    

    Subscribe to webView's delegate

    This will let you know (your controller when your webView has finished loading the contents of your site.

    self.webView.navigationDelegate = self
    

    Conform to protocol

    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
            }
        }
    }
    

    Finally, fix your constraints.

    a. Remove your imageView bottom constraint. b. Add bottom constraint to your save button to the scrollView.

    This will make your scrollView scrollable.

    Voila! This yields this result.

    I hope this helps!

提交回复
热议问题