Search code examples
iosxamarinthemesdarkmode

How can I get a Xamarin Application (not Forms) to Read the Dark or Light Theme in Settings?


When I attempt this:

https://learn.microsoft.com/en-us/xamarin/essentials/app-theme?tabs=ios

appTheme is always Light, regardless of my simulator or device Dark setting.

I also tried this:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes

But I can't find the code that fetches the Settings theme.

Your help is appreciated!

Thanks! Larry


Solution

  • UIKit.TraitCollection.UserInterfaceStyle contains the current app|view dark/light setting

    if (this.TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
    {
       // dark
    }
    else
    {
      // light
    }
    

    re: https://learn.microsoft.com/en-us/dotnet/api/UIKit.UITraitCollection.UserInterfaceStyle?view=xamarin-ios-sdk-12

    re: https://learn.microsoft.com/en-us/dotnet/api/uikit.uiuserinterfacestyle?view=xamarin-ios-sdk-12

    Update:

    "Still always returning light."

    If your info.plist contains an UIUserInterfaceStyle entry that is set to Light it will override the OS setting. Either remove this entry or set it to Automatic.