无法更改 WKWebView 中 UIScrollview 的内容大小

Unable to change contentSize of UIScrollview in WKWebView

我正在尝试通过调整 contentSize [=29] 来为 WKWebView 内的 UIScrollView 的内容添加一点额外的高度=].

我可以修改这个 属性,但在下一次 layout/display 刷​​新命中时,它会以某种方式一直变回原来的大小。

为了进一步测试,我尝试更改 scrollViewDidScroll 中的 contentSize。每当您滚动到底部时,您会在几分之一秒内看到它正在尝试添加额外的 space 并不断返回。

我无法用 UIWebView 重现这个问题。它在那里工作得很好。也许最近对 WKWebView 添加了一些更改?我正在使用 Xcode 8 并在 iOS 9/10 上进行测试。

鉴于我对 Dropbox 的无能,我感觉很糟糕,所以将附件放在一起尝试帮助您。如果您更改 WKWebView 的 scrollView 的 contentInset 属性 而不是 contentSize,这似乎工作得很好。我同意你的看法,虽然你可以暂时更改 scrollView 的内容大小,但它会很快恢复;此外,我发现 UIScrollView 或 WKWebView 都没有委托方法,您可以重写以抵消这种情况。

以下示例代码包含一个网页和一些按钮,这些按钮允许您增加或减少顶部和底部的 contentInset 并以动画方式显示到 scrollView 上的适当位置。

import UIKit
import WebKit

class ViewController: UIViewController {

    var webView : WKWebView!
    var upButton : UIButton!
    var downButton : UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let webFrame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: self.view.frame.width - 200, height: self.view.frame.height - 200))
        webView = WKWebView(frame: webFrame)
        webView.load(URLRequest(url: URL(string: <PUT RELEVANT URL STRING (NB - THAT YOU ARE SURE IS VALID) HERE>)!))
        webView.backgroundColor = UIColor.red
        webView.scrollView.contentMode = .scaleToFill
        self.view.addSubview(webView)

        func getButton(_ label: String) -> UIButton {
            let b : UIButton = UIButton()
            b.setTitle(label, for: .normal)
            b.setTitleColor(UIColor.black, for: .normal)
            b.layer.borderColor = UIColor.black.cgColor
            b.layer.borderWidth = 1.0

            return b
        }

        let upButton = getButton("Up")
        let downButton = getButton("Down")

        upButton.frame = CGRect(origin: CGPoint(x: 25, y: 25), size: CGSize(width: 50, height: 50))
        downButton.frame = CGRect(origin: CGPoint(x: 25, y: 100), size: CGSize(width: 50, height: 50))

        upButton.addTarget(self, action: #selector(increaseContentInset), for: .touchUpInside)
        downButton.addTarget(self, action: #selector(decreaseContentInset), for: .touchUpInside)

        self.view.addSubview(webView)
        self.view.addSubview(upButton)
        self.view.addSubview(downButton)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func increaseContentInset() -> Void {
        guard let _ = webView else { return }

        webView.scrollView.contentInset = UIEdgeInsetsMake(webView.scrollView.contentInset.top + 100, 0, webView.scrollView.contentInset.bottom + 100, 0)

        webView.scrollView.setContentOffset(CGPoint(x: webView.scrollView.contentInset.left, y: -1 * webView.scrollView.contentInset.top), animated: true)

    }

    func decreaseContentInset() -> Void {
        guard let _ = webView else { return }

        webView.scrollView.contentInset = UIEdgeInsetsMake(webView.scrollView.contentInset.top - 100, 0, webView.scrollView.contentInset.bottom - 100, 0)

        webView.scrollView.setContentOffset(CGPoint(x: webView.scrollView.contentInset.left, y: -1 * webView.scrollView.contentInset.top), animated: true)
    }
}

希望对您有所帮助。如果您需要专门基于设置内容大小的答案,请告诉我,但我认为这是最佳选择。