2.4 KiB
title = "Re-Fixing WKWebView Scroll Indicators"
tags = ["swift"]
date = "2022-01-30 21:23:42 -0400"
short_desc = "Swizzling wasn't worth it, I should have just waited a few more weeks."
slug = "wkwebview-scroll-indicators-again"
As my luck would have it, just a few weeks after I published my last post on this topic, the iOS 15.4 beta came out which broke that hack and once again made my scroll indicators invisible in dark mode.
Some time ago, a bug was filed against WebKit because setting scrollIndicatorStyle
on a web view's scroll view was broken on iOS 15. The fix for this bug landed in iOS 15.4 and it subtly changed the behavior of WKScrollView when it comes to the indicator style.
The bug was fixed by tracking whether the web view client has overriden the scroll indicator style and, if so, blocking the web view from resetting it internally. Unfortunately, it does this by checking if the new indicator style is not .default
. So, even if you set it to .default
to make it automatically switch based on system appearance, the scroll view will interpret that to mean the indicator style hasn't been overriden and continue erroneously setting it based on background color (or, in my case, the non-opaqueness of the web view).
The solution is simple, if annoying. You need to check the current user interface style and select the appropriate scroll indicator style yourself.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateScrollIndicatorStyle()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
updateScrollIndicatorStyle()
}
private func updateScrollIndicatorStyle() {
guard #available(iOS 15.4, *) else {
// different workaround pre-iOS 15.4
return
}
if traitCollection.userInterfaceStyle == .dark {
webView.scrollView.indicatorStyle = .white
} else {
webView.scrollView.indicatorStyle = .black
}
}
And, if you, like me were previously using the old, swizzling workaround, you need to disable in on iOS 15.4. If the old workaround remains active, studiously setting the indicator style to .default
whenever WebKit would override it, it would merely undo all of our hard work.