How to change status bar color on iOS 13?

MikilllMikilll Member ✭✭✭

Hello,
how to change status bar color on iOS 13?

On previous versions of iOS I was using this code:

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
    statusBar.BackgroundColor = UIColor.White;
}

However, on iOS 13 an app crashes when you execute this code.

Answers

  • LukeBailey.9712LukeBailey.9712 USMember ✭✭
    UIView statusBar = UIDevice.CurrentDevice.CheckSystemVersion(13, 0) ? new UIView(UIApplication.SharedApplication.StatusBarFrame) : UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
    

    Worked for me

  • LandLuLandLu Member, Xamarin Team Xamurai

    on iOS 13 you can't retrieve status bar through UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")).
    You need to create it by yourself and add it to the window like:

    if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
    {
        UIWindow keywindow = null;
        foreach (UIWindow window in UIApplication.SharedApplication.Windows)
        {
            if (window.IsKeyWindow)
            {
                keywindow = window;
                break;
            }
        }
        if (keywindow != null)
        {
            int tag = 100;
            UIView statusBar = keywindow.ViewWithTag(tag);
            if (statusBar == null)
            {
                statusBar = new UIView(keywindow.WindowScene.StatusBarManager.StatusBarFrame);
                statusBar.BackgroundColor = UIColor.Red;
                statusBar.Tag = tag;
                keywindow.AddSubview(statusBar);
            }
        }
    }
    else
    {
        ...
    }
    
  • NicolasKrierNicolasKrier FRMember ✭✭✭
    edited October 23

    @LukeBailey.9712 said:

    UIView statusBar = UIDevice.CurrentDevice.CheckSystemVersion(13, 0) ? new UIView(UIApplication.SharedApplication.StatusBarFrame) : UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
    

    Worked for me

    Worked for me very well :)
    Thanks

    @Mikilll can you mark the answer as the solution to help other ?
    In addition, it will be nice for the people who tried to help you ;) don't forget they did it for free.

Sign In or Register to comment.