0

In my AppDelegate I have a way to change the UIUserInterfaceStyle:

[Register("AppDelegate")]
public partial class AppDelegate :     global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
  {
  public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        MessagingCenter.Subscribe<Page, bool>(this, "ModeChanged", callback: OnModeChanged);
        LoadApplication(new App());
        return base.FinishedLaunching(app, options);
    }
    private void OnModeChanged(Page page, bool IsDarkModeEnabled)
    {
        if (IsDarkModeEnabled)
            Window.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Dark;
        else
            Window.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
    }

}

What I would like to do is to make this change before the storyboard appears as I want to change the storyboard color, by checking some stored data (I'm not sure what I could check).

Is there a method that I could before the storyboard appears and is there a way in that method that I could access stored information that could tell me if my app is running in it's own internal light or dark mode?

Alan2
  • 23,493
  • 79
  • 256
  • 450

2 Answers2

1

You can use TraitCollectionChanged in the page per the documentation.

public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
        {
            base.TraitCollectionDidChange(previousTraitCollection);

            App.ApplyTheme();
        }

Reference :https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/dark-mode

Since you have marked this as Xamarin forms I am assuming you are making use Xamarin forms page,

You can put that code in OnAppearing() override method of page to do it before page loads visually. To call native method make use of DependencyService

override void OnAppearing() {

var ui=DependencyService.Get<IUserInterface>();
ui?.CallUIStyle();
class IosUserInterface: IUserInterface
{
    void CallUIStyle() 
    {

    Window.OverrideUserInterfaceStyle = //

   } 
} 

CallUIStyle should have that Window OverloadUserInterface implementation.

Morse
  • 8,258
  • 7
  • 39
  • 64
0

Your question is confusing because it doesn’t mention which storyboard. Every app has a launch screen storyboard, and some others prefer to also have a splash screen storyboard added after the launch screen.

You cannot run any code only before the launch screen, But you can create a splash screen that looks like the launch screen and so it appears that you are on the same screen, Which is what many of the new apps do.

This is not just a limitation of Xamarin. Xamarin uses native development, and severe found by the limitations of native iOS developerThis is not just a limitation of Xamarin. Xamarin uses native development, and severe found by the limitations of need of iOS development.

Here’s some reference for you: https://stackoverflow.com/a/29623961/11104068

Saamer
  • 4,687
  • 1
  • 13
  • 55