1

I am using the new concept AppShell, and I am doing the following flow

    App.MainPage = new AppShell();

then I do

    protected async override void OnStart()
    {
        await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
    }

With this I have a ForgetPasswordPage, from LoginViewModel I do

   await Shell.Current.GoToAsync($"//{nameof(ForgetPasswordPage)}");

The point is if I do this

    var color = new Color(33, 150, 243);
    Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(color);

Inside the Create method from Android the StatusBar has the color I defined. Now where is the method for iOS, like this?

I tried theses solutions

Without succeed...

Someone can explain, how to change the status code but I would like to set it inside my ContentPage, I could use a style, or a simple line of code like this

Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(color);

Which is used in Android project.

I did the steps from the other posts, and I got:

Foundation.MonoTouchException: 'Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead. Native stack trace: 0 CoreFoundation 0x00007fff20422fba __exceptionPreprocess + 242

System.Reflection.TargetInvocationException Message=Exception has been thrown by the target of an invocation

saramgsilva
  • 734
  • 1
  • 7
  • 17

1 Answers1

5

You can take a look at my video here that walks through in details: https://www.youtube.com/watch?v=GKJRR8_DSSs

In general:

Android -

        public void SetStatusBarColor(System.Drawing.Color color, bool darkStatusBarTint)
        {
            if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop)
                return;

            var activity = Platform.CurrentActivity;
            var window = activity.Window;
            window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);
            window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
            window.SetStatusBarColor(color.ToPlatformColor());

            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
            {
                var flag = (Android.Views.StatusBarVisibility)Android.Views.SystemUiFlags.LightStatusBar;
                window.DecorView.SystemUiVisibility = darkStatusBarTint ? flag : 0;
            }
        }

iOS:

 public void SetStatusBarColor(System.Drawing.Color color, bool darkStatusBarTint)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                var statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
                statusBar.BackgroundColor = color.ToPlatformColor();
                UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
            }
            else
            {
                var statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
                if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
                {
                    statusBar.BackgroundColor = color.ToPlatformColor();
                }
            }
            var style = darkStatusBarTint ? UIStatusBarStyle.DarkContent : UIStatusBarStyle.LightContent;
            UIApplication.SharedApplication.SetStatusBarStyle(style, false);
            Xamarin.Essentials.Platform.GetCurrentUIViewController()?.SetNeedsStatusBarAppearanceUpdate();
        }
JamesMontemagno
  • 3,782
  • 1
  • 12
  • 13