3

I have an iOS app that had its own theming system (dark/light UI styling) before iOS 13 introduced it natively. This is partly achieved via the UIAppearance methods on various UI elements, like bar button items, segmented controls, and such.

To integrate my themes with the iOS light and dark styles, I'd like to automatically switch my themes when the system changes between dark and light mode. I can accomplish this mostly by implementing traitCollectionDidChange on various view and view controller classes and calling my own theme update methods if the UI style has changed. But I also need to trigger an update of the UIAppearance setup, and I'd like to do this only once per system UI style change.

My question: how do I know centrally, in one place, when the iOS system light/dark mode has changed? I'm hoping for the color appearance equivalent of UIContentSizeCategoryDidChangeNotification. This would also mean less repetitive code implementing traitCollectionDidChange everywhere.

I could trigger it on traitCollectionDidChange in my root view controller, but once I implement multi-scene support for iPadOS, I no longer really have one "root" view controller.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • 1
    Have you tried using the `UIAppearance` methods [`appearanceForTraitCollection:`](https://developer.apple.com/documentation/uikit/uiappearance/1615007-appearancefortraitcollection?language=objc) or [`appearanceForTraitCollection:whenContainedInInstancesOfClasses:`](https://developer.apple.com/documentation/uikit/uiappearance/1615008-appearancefortraitcollection?language=objc), given a trait collection with `userInterfaceStyle` set to light or dark? Then you could set up your `UIAppearance` variations for both light and dark, once, up front. – Kurt Revis Jun 26 '19 at 07:09
  • 1
    @KurtRevis yeah, I'm experimenting with that right now - that might solve the reason behind this question. But getting a single notification on change would still simplify things. – Tom Hamming Jun 26 '19 at 14:58
  • I am in exactly the same situation as you @TomHamming Did you find a solution for this ? – Brett Nov 15 '19 at 06:38

0 Answers0