6

When darkmode is enabled, UIViewControllers always use light mode. viewController.traitCollection.userInterfaceStyle is always Light but window.traitCollection.userInterfaceStyle is Dark.

UIUserInterfaceStyle is not set in plist. I tried all possible settings - adding UIUserInterfaceStyle to plist, setting overrideUserInterfaceStyle for UIWindow and for UIViewController. I logged userInterfaceStyle for different cases and results are

TestDarkModeViewController *testController = [[TestDarkModeViewController alloc] initWithNibName:@"TestDarkModeViewController" bundle: nil];
testController.traitCollection.userInterfaceStyle //result Light

but for

UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:testController];
navController.traitCollection.userInterfaceStyle //result Dark

UIViewController *testController2 = [[UIViewController alloc]init];
testController2.traitCollection.userInterfaceStyle //result Dark

It seems it happens only when controller is programatically loaded from xib file. Any idea what can be wrong and how to solve this?

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63
  • this might help https://stackoverflow.com/questions/57567415/uilabel-in-uitableviewcell-in-xib-file-ignores-dark-mode https://developer.apple.com/forums/thread/122169 – Mayur Tanna Jul 19 '21 at 16:45
  • thanks but it didn't help – Martin Vandzura Jul 20 '21 at 05:11
  • can you share code sample you are trying, I did the same in Swift , it works as expected. – Mayur Tanna Jul 20 '21 at 08:24
  • I have it only in 1 app which I can't share. It works in any other apps. What I just figured out during is that if I overrideUserInterfaceStyle = dark in viewcontroller it doesn't work, but if in viewcontroller if I do view.overrideUserInterfaceStyle = dark then it works. I ended up with [[UIView appearance] setOverrideUserInterfaceStyle:Dark]; in appdelegate which seems to work. But still no idea why it doesn't work otherwise – Martin Vandzura Jul 20 '21 at 08:44
  • Share the `TestDarkModeViewController` – Mojtaba Hosseini Jul 23 '21 at 16:38

1 Answers1

0

Set this property programmatically

 navController.overrideUserInterfaceStyle = //.light/.dark

and you can also forcefully set windows property.

window.overrideUserInterfaceStyle = //.light/.dark
SAXENA
  • 423
  • 1
  • 4
  • 17
  • it doesn't work. I already tried that. Only thing which seems to work is [[UIView appearance] setOverrideUserInterfaceStyle:Dark]; – Martin Vandzura Jul 26 '21 at 11:32