When an app runs on iOS 13 and when a user flips to dark mode from settings then apps do not look so great. So there are two ways to handle this: either create different themes for Dark Mode and Light Mode and switch based on the system’s theme at runtime or prevent the Dark Mode. So In this article, I’m going to show you how to Prevent Dark Mode in Xamarin Forms.
Dark Mode is introduced in iOS 13 and In Android It’s introduced in Android 10 (API Level 29) in android it is called Dark Theme.
Let’s Start
To force the light theme in application we need to do some changes in native side.
In iOS Side :
Add the following key to info.plist file :
<key>UIUserInterfaceStyle</key> <string>Light</string>
We can also do this by creating custom renderer for either ContentPage or NavigationPage as follows :
Using ContentPage Renderer :
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))] namespace Example.iOS { public class CustomPageRenderer : PageRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light; } } }
Using NavigationPage Renderer :
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNaviPageRenderer))] namespace Example.iOS { public class CustomNaviPageRenderer : NavigationRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light; } } }
In Android Side :
We have to put the below code to OnCreate() method in MainActivity :
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
That’s all for now!
Happy Coding! 😀
You may also like
How to get Device and App Informations in MAUI
How to use UniformItemsLayout in MAUI