Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms: Disabled entry has white TextColor when Dark Mode is enabled on iOS

I seem to have some troubles with the TextColor of my Entry on iOS when Dark Mode is enabled. Whenever I set the Entry's Enabled state to false, the TextColor turns white and it's not possible to change. I used the following simple code to reproduce this.

Page.xaml

    <Entry x:Name="TestEntry" />
Page.xaml.cs

    public OnboardingPage()
    {
        InitializeComponent();

        TestEntry.Text = "Testo";
        TestEntry.TextColor = Color.Blue;
        TestEntry.IsEnabled = false;
    }

I am currently using the latest version of Xamarin.Forms. (4.4.0.991640)

Does anybody have an idea what's going wrong here? I don't think this is expected behavior..

Thanks in advance!

like image 784
Brent Van Vosselen Avatar asked Feb 20 '20 12:02

Brent Van Vosselen


People also ask

How does xamarin form handle dark mode?

Set the current user themeUserAppTheme = OSAppTheme. Dark; In this example, the application is set to use the theme defined for the system dark mode, regardless of which system theme is currently operational. Set the UserAppTheme property to OSAppTheme.


2 Answers

In the meanwhile I have found the fix necessary to my question.

Apparently the EntryRenderer on iOS uses uses the default color when legacy color management is enabled and the Entry is disabled. Setting the legacyColorManagement to False on the Entry solves this issue.

XF check for legacycolormanagement https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Extensions/VisualElementExtensions.cs#L15

XF check for TextColor of the Entry https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Renderers/EntryRenderer.cs#L272

I've added following code to my *Page.xaml and now the colors are rendering correctly:

*Page.xaml

<ContentPage xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ...>

<Entry ios:VisualElement.IsLegacyColorModeEnabled="False"
       .../>

like image 154
Brent Van Vosselen Avatar answered Oct 16 '22 05:10

Brent Van Vosselen


The reason that is happening because of how iOS works with the dark mode for you to handle this you have to add the following in your Info.Plist

<key>UIUserInterfaceStyle</key>
<string>Light</string>

What this will do is stop your app from changing anything when Dark mode is turned on i.e. it is sort of an opt-out

Goodluck

Feel free to get back if you have questions

like image 44
FreakyAli Avatar answered Oct 16 '22 05:10

FreakyAli