traitCollectionDidChange Triggered Twice In iOS17

In iOS17, traitCollectionDidChange gets triggered twice on iOS17 devices/simulators, while on iOS<17 devices/simulators, it only gets triggered once. I was wondering if there was a reason for this. I'm currently investigating issues between portrait and landscape and UI/view controllers looking weird because of the rotation and trying to figure out what the issue is. I found this thread https://developer.apple.com/forums/thread/732069 which makes me think that code should still work, but looking for any feedback that could help. Thanks!!

Replies

-traitCollectionDidChange: is called any time that the traitCollection of that object has changed. This includes changes to any traits, including built-in system traits, your own custom traits (using the new APIs in iOS 17), or other custom traits from other libraries/frameworks whose code is running in your app. (This is why -traitCollectionDidChange: is deprecated in iOS 17: because it is called for any and all trait changes, it does not scale as more and more traits are added — your code only cares about changes to the small subset of traits it actually uses.)

Often, multiple trait changes will be coalesced into a single -traitCollectionDidChange: call. However, there can also be cases where -traitCollectionDidChange: is called multiple times, because one or more traits are invalidated (e.g. trait overrides are changed), traits are updated and the change notifications are sent, and then more trait(s) are invalidated afterwards.

The best place to start is to use the trait collection change logging that was introduced in iOS 13, which was mentioned during the Implementing Dark Mode on iOS session from WWDC19 starting at this timestamp. Set the launch argument -UITraitCollectionChangeLoggingEnabled YES in your app's scheme to enable it, then filter the logs for category TraitCollectionChange and run your app.

If there is a situation where you are seeing unexpected behavior, please submit a feedback report that includes a sample project that reproduces the issue you're seeing, so we can investigate and understand what is causing the behavior you're observing.