如果我在显示WKWebView时打开键盘,它会自动将adjustedContentInset添加到ScrollView中。但问题是,如果我自己处理键盘,它仍然会添加adjustedContentInset。我该如何解决这个问题?
NotificationCenter.default.removeObserver(self.webView, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.removeObserver(self.webView, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self.webView, name: UIResponder.keyboardWillHideNotification, object: nil)
在更改 Web 视图的自动布局约束时,当 keyboardWillShowNotification
/ keyboardWillHideNotification
触发时,我遇到了这个问题。
对我有效的方法:
WKWebView
。let notificationCenter = NotificationCenter.default
notificationCenter.publisher(for: UIResponder.keyboardWillShowNotification, object: nil)
.sink { [weak self] _ in
self?.attachWebViewToKeyboard()
self?.view.layoutIfNeeded()
}
.store(in: &cancellables)
notificationCenter.publisher(for: UIResponder.keyboardWillHideNotification, object: nil)
.sink { [weak self] _ in
self?.attachWebViewToBounds()
self?.view.layoutIfNeeded()
}
.store(in: &cancellables)
webView = WKWebView(frame: .zero)