traitCollectionDidChange not triggered after setting overrideUserInterfaceStyle to .unspecified

Hello everyone, I'm trying to allow a dynamic mode selection (dark/light/system) to my app. I have managed to create the correct behaviour when I set overrideUserInterfaceStyle to either .dark or .light, and I see the method traitCollectionDidChange being called in the UIViewController. When I want the app to go back to the system settings though, I set the overrideUserInterfaceStyle to .unspecified but traitCollectionDidChange is not triggered. Is this standard behaviour? And if so, do you have any suggestions on how to trigger traitCollectionDidChange after setting overrideUserInterfaceStyle to .unspecified?

Thank you very much, Dave

Just a check first. Have you a log inside traitCollectionDidChange to check it is effectively not called (and not only does nothing) ?

Did you try calling directly:

traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)

with the previousTraitCollection or nil after setting overrideUserInterfaceStyle to .unspecified ?

Yes I debugged with a breakpoint and it's not being called, I haven't tried calling it directly no, I'll give it a try, just wondering if it's needed though because it seems to do it automatically when I set overrideUserInterfaceStyle to the other two possible values.

I need to add, this only happens the first time overrideUserInterfaceStyle is set to .unspecified when i open the app. If I switch to either .dark or .light and then back to .unspecified, traitCollectionDidChange is called.

I think I found the problem, when the app starts the value of overrideUserInterfaceStyle is set to .unspecified by default, and even though the visualization was correct because we stored the current mode in a variable in the perstistent container, we were not updating overrideUserInterfaceStyle to the correspondent value at startup.

traitCollectionDidChange not triggered after setting overrideUserInterfaceStyle to .unspecified
 
 
Q