Skip to content

Learn Mobile Development

Alamgeer's Blog

Menu
  • Home
  • Privacy Policy
Menu
Prevent Dark Mode in Xamarin forms

How to Prevent Dark Mode in Xamarin forms

Posted on February 3, 2023February 3, 2023 by Learn Mobile Development

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

get device and app informations in maui

How to get Device and App Informations in MAUI

UniformItemsLayout in MAUI

How to use UniformItemsLayout in MAUI

Post Views: 988
Share article with

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I'm Alamgeer Ashraf and having 9+ years of experience in enterprises mobile application development with Xamarin Native, Xamarin Forms & .Net MAUI.

Archives

  • February 2023 (1)
  • January 2023 (9)
  • December 2022 (5)
  • November 2022 (6)

Latest Posts

  • Prevent Dark Mode in Xamarin forms
    How to Prevent Dark Mode in Xamarin formsFebruary 3, 2023
  • apply color to Images
    How to apply color to Images and Icons in MAUIJanuary 30, 2023
  • iOS Large Page Title
    How to set iOS large page title in MAUIJanuary 26, 2023
  • Change StatusBar Color
    How to change StatusBar color in MAUIJanuary 22, 2023
  • Item reordering to CollectionView
    How to apply item reordering to CollectionView GridItemsLayout in MAUIJanuary 18, 2023

Popular Posts

  • How to apply color to Images and Icons in MAUI
  • How to get Device and App Informations in MAUI
  • How to use MediaElement in Xamarin Forms

Latest Comments

  1. Randyperly on How to use StateContainer in MAUIOctober 18, 2024

    very good _________________ netgame casino бездепозитный бонус

  2. SmiSew on How to use StateContainer in MAUIOctober 10, 2024

    Thanks for the post _________________ pin up casino бест

  3. StavkiTarve on How to use StateContainer in MAUIOctober 7, 2024

    interesting post _________________ слоты в 1xbet приложение

  4. Luke on How to use drag and drop gesture to CollectionView in MAUISeptember 25, 2024

    Thanks very much!

  5. Tony - Nhan Nguyen Lhoa Minh on How to apply item reordering to CollectionView GridItemsLayout in MAUIMarch 28, 2024

    That would be an amazing solution. It's easy to comprehend. Thank you very much.

Our Visitor

0 0 7 9 0 6
Users Today : 10
Users Yesterday : 4
Users Last 7 days : 39
Users Last 30 days : 134
Users This Month : 127
Total views : 14144
How to use UniformItemsLayout in MAUI
Trending
How to use UniformItemsLayout in MAUI

Category

  • .Net MAUI
  • MAUI Community Toolkit
  • MVVM
  • Xamarin Community Toolkit
  • Xamarin Forms

Contact Me

  • LinkedIn
      © 2025 Learn Mobile Development