I have a bunch of colors that I'm updating to support the new iOS 13 dark mode. I'm using them programmatically, with the new dynamic provider block constructor. The problem here is that sometimes the block is called with the wrong value for the userInterfaceStyle
property, causing the color to return the wrong value (sometimes dark instead of light, sometimes light instead of dark).
From my understanding, I don't need to listen for traitCollection
changes or anything of the sorts with this new constructor. It should do it automatically (or am I wrong?).
The funny thing here is that I tried doing it using some dummy colors through the assets catalog and it also does not work. Also tried using some of the new system provided dynamic colors, like .systemBackgroundColor. It also resolves the color wrong sometimes.
if #available(iOS 13.0, *) {
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
if traitCollection.userInterfaceStyle == .dark {
return darkColor
}
else {
return lightColor
}
}
}
So, what exactly is supposed to happen? Should this work as I'm expecting or do I absolutely need to assign the colors in a specific place (like inside viewWillLayoutSubviews
or traitCollectionDidChange
)?
Resolving the color manually, using .resolvedColor(with: UITraitCollection)
works. But from what I understood from the documentation, I should not need to resolve it manually like this.
Ok, so I found the source of the problem. When setting dynamic colors to a navigationBar
or to a searchBar
, pay attention to the barStyle
attribute of the view. I was using .dark
for the navigationBar (to display the status bar with white text). That was causing the UIColor dynamic provider to resolve the wrong color. Changing it to .default
fixed it for me.
For the searchBar
, I wasn't setting anything specific. Forcing it to .default
also fixed it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With