如果设置了contentInset,则长按以选择文本时WKWebView奇怪的滚动行为

问题描述 投票:0回答:1

我有一个问题,长按并在WKWebView上选择文本将随机滚动Web视图。当我设置Web视图的滚动视图的contentInset.top属性时,会发生此行为。

[截图]

  1. 长按文字。绿色区域是当地的UIView https://ibb.co/P4LqgBB
  2. 拖动文本后,WKWebView意外地向下滚动https://ibb.co/r5KR4JB

由于我的应用程序需要在Web视图部件上方显示本机视图,因此当用户需要从Web视图复制和粘贴文本时,这种行为会让用户感到沮丧。

以下是您可以用来重现该问题的最低代码。我在iPhone 8 iOS 12.2上使用Xcode 10.2尝试了这个。设置webView.scrollView.contentInset.top = 100时会出现此问题。此外,如果您将值更改为1000,其中contentInset.top比手机的屏幕大小更长,则长按将导致Web视图立即滚动。

 override func viewDidLoad() {
        super.viewDidLoad()

        // Create WKWebView
        let webView = WKWebView(frame: .zero)
        webView.translatesAutoresizingMaskIntoConstraints = false
        webView.scrollView.contentInsetAdjustmentBehavior = .never
        webView.clipsToBounds = false
        webView.scrollView.bounces = false

        // Create Native UIView
        let nativeView = UIView(frame: .zero)
        nativeView.translatesAutoresizingMaskIntoConstraints = false
        nativeView.backgroundColor = .green

        // Add WebView to the view
        view.addSubview(webView)

        webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        webView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        webView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        // Set contentInset to give blank space for native view
        webView.scrollView.contentInset.top = 100

        // Add the native view as webView scrollView's child
        webView.scrollView.addSubview(nativeView)
        nativeView.leadingAnchor.constraint(equalTo: webView.leadingAnchor).isActive = true
        nativeView.trailingAnchor.constraint(equalTo: webView.trailingAnchor).isActive = true
        nativeView.topAnchor.constraint(equalTo: webView.scrollView.topAnchor,
                                        constant: -100).isActive = true
        nativeView.heightAnchor.constraint(equalToConstant: 100).isActive = true

        // Load the webpage
        let url = URL(string: "https://www.apple.com")!
        let request = URLRequest(url: url)
        webView.load(request)
    }

我希望长按和滚动的行为就像没有设置contentInset.top时那样。

有谁知道如何解决这个问题?

ios swift wkwebview
1个回答
0
投票

如果css不够干净,WKWebView会产生很多问题。

我建议回到WebView,它应该解决你所有的问题

© www.soinside.com 2019 - 2024. All rights reserved.