更改 WKWebview 框架高度后如何删除 WKScrollView 内的空白 space?

How to remove blank space inside WKScrollView after change WKWebview frame height?

如何去除WKWebview的frame height改变后的空白区域?
我想在键盘出现时更改 webview 的高度,但在框架高度更改后,HTML 正文下方有一个额外的 space。并且这个额外的空白区域的高度大约是键盘的高度。

这是代码片段:

NotificationCenter.default.rx
.notification(UIApplication.keyboardDidShowNotification)
.subscribe(onNext: { [weak self] keyboardConfig in
    if let userInfo = keyboardConfig.userInfo{
       let keyboardBounds = (userInfo["UIKeyboardFrameEndUserInfoKey"] as! NSValue).cgRectValue;
                
       self?.webView.snp.remakeConstraints({ make in
           make.left.right.top.equalToSuperview();
           /// remake from screen height to below height.
           make.height.equalTo(UIScreen.main.bounds.size.height - keyboardBounds.size.height);
       });
    }
});

最后我仍然不知道如何删除这个多余的空白区域,但我找到了解决方法。
由于 WKScrollViewUIScrollView 的子类,因此 UIScrollView Delegate 也适用于 WKScrollView
因此,可以在 UIScrollView scrollViewDidScroll 委托方法中强制重置 contentOffset。下面提供了代码片段。

/// Change webview frame when keyboard appear.
NotificationCenter.default.rx
.notification(UIApplication.keyboardDidShowNotification)
.subscribe(onNext: { [weak self] keyboardConfig in
    if let userInfo = keyboardConfig.userInfo, let this = self {
        let keyboardBounds = (userInfo["UIKeyboardFrameEndUserInfoKey"] as! NSValue).cgRectValue;
        this.webView.snp.remakeConstraints({ make in
            make.left.right.top.equalToSuperview();
            /// remake from screen height to below height.
            make.height.equalTo(UIScreen.main.bounds.size.height - keyboardBounds.size.height);
        });
        let originalOffset = this.webView.scrollView.contentOffset;
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            this.webView.scrollView.contentOffset = originalOffset;
        }
    }
});
/// Force reset contentOffset when the contentOffset is out of HTML Body
extension YourWebviewVC: UIScrollViewDelegate{
    internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.adjustWebviewOffsetFor(scrollView);
    }
    
    private func adjustWebviewOffsetFor(_ scrollView: UIScrollView, animated: Bool = false){
        let currentY = scrollView.contentOffset.y + scrollView.frame.height;
        if currentY > scrollView.contentSize.height{
            let maxOffset = scrollView.contentSize.height - scrollView.frame.height;
            scrollView.setContentOffset(CGPoint(x: 0, y: maxOffset), animated: animated);
        }
    }
}
/// Don't forgot set webview scroolView's delegate, for example
self.webView.scrollView.delegate = self;
self.webView.scrollView.showsVerticalScrollIndicator = false;