WKWebView weird spacing issue in iOS 13
WKWebView with iOS 12 and 13 has weird spacing issue when keyboard shows up, which can lead to html footer display error.
Below pictures:
Left: Weird spacing in the bottom; Right: What it should have been
While it seems there is no solution in StackOverFlow, here is one possible workaround you may consider.
- Set ScrollView inset to
self.automaticallyAdjustsScrollViewInsets = false
if #available(iOS 11.0, *) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
}
2. Set inset to .zero whenever the spacing shows up.
2.1 Store the content offset when Keyboard shows
func onKeyboardShows(){
if #available(iOS 12.0, *) {
lastContentOffset = webView.scrollView.contentOffset
}
}
2.2
func onKeyboardHides(){
if #available(iOS 12.0, *) {
if let offset = lastContentOffset {
if !__CGPointEqualToPoint(offset, webView.scrollView.contentOffset) {
print(“Reset webview offset”)
webView.scrollView.setContentOffset(offset, animated: true)
webView.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
lastContentOffset = nil
}
}
}
}
Now it should be able to animate back to where it should be.
To make it more robust, remember to handle the case of multiple text fields.
Happy Coding. 🙂
Find me in Twitter: @rick2817
Credit: