简体   繁体   English

滚动视图内的 WKWebView

[英]WKWebView inside Scroll View

How can I put a WKWebView inside a Scroll View consider that I must have others elements after Web View.如何将 WKWebView 放在滚动视图中,考虑到在 Web 视图之后我必须有其他元素。

. View ... Scroll View ....... ImageView ....... WKWebView ....... Button

See the demo image查看演示图片

  • Issue 1: Disable scroll for WKWebView问题 1:禁用 WKWebView 的滚动
  • Issue 2: Set full content height for WKWebView问题 2:为 WKWebView 设置完整内容高度

Check my sample project检查我的示例项目

If you mean to get the content height of the html in WKWebView to make it non-scrollable inside UIScrollView , then read on.如果您想在WKWebView获取 html 的内容高度以使其在UIScrollView内不可滚动,请继续阅读。

You need to observe (via delegate) the content height of your webView.您需要观察(通过委托)您的 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从您的 webView 高度约束中创建一个出口

@IBOutlet weak var constraint_WebViewHeight: NSLayoutConstraint!

Subscribe to webView's delegate订阅 webView 的委托

This will let you know (your controller when your webView has finished loading the contents of your site.这会让您知道(当您的 webView 完成加载您网站的内容时,您的控制器。

self.webView.navigationDelegate = self

Conform to protocol符合协议

Inside the didFinish navigation method of WKNavigationDelegate , you then adjust the constant of your webView height constraint.WKNavigationDelegatedidFinish navigation方法中,然后调整 webView 高度约束的常量。 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.删除您的 imageView 底部约束。 b.Add bottom constraint to your save button to the scrollView.将底部约束添加到滚动视图的保存按钮。

This will make your scrollView scrollable.这将使您的 scrollView 可滚动。

Voila!瞧! This yields this result.这产生了这个结果。

在此处输入图片说明

I hope this helps!我希望这有帮助!

As per the understanding you want webview not to scroll well if the page is bigger than your screen it will scroll right?根据您的理解,如果页面大于屏幕,您希望 webview 不能很好地滚动,它会正确滚动吗? To show the full page.显示整页。 Although you can change the isscrollingenabled to false from interface builder or from code by using object of scrollview.尽管您可以使用滚动视图的对象从界面构建器或代码中将 isscrollingenabled 更改为 false。 To set full height give constraint 0,0,0,0 from all sides and it will surely stretch to whole screen要设置全高,从四面八方给约束 0,0,0,0,它肯定会拉伸到整个屏幕

It's wrong approach.这是错误的做法。 You should use WKWebView's scrollView.您应该使用 WKWebView 的 scrollView。

You need reserve space for image and button with html margins using javascript您需要使用 javascript 为带有 h​​tml 边距的图像和按钮保留空间

[NSString stringWithFormat:@"document.body.style.margin = '%dpx %dpx %dpx %dpx'", (int)margins.top, (int)margins.right, (int)margins.bottom, (int)margins.left]

or directly in html body if you able to access it.或直接在 html 正文中,如果您可以访问它。

Add imageview and button into WKWebView's scrollView.将 imageview 和 button 添加到 WKWebView 的 scrollView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM