locked
How to change status bar color on iOS 13? RRS feed

  • Question

  • User369433 posted

    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.

    Thursday, October 17, 2019 4:35 PM

All replies

  • User237726 posted

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

    Worked for me

    Thursday, October 17, 2019 4:53 PM
  • User369979 posted

    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
    {
        ...
    }
    
    Friday, October 18, 2019 2:48 AM
  • User93655 posted

    @"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.

    Wednesday, October 23, 2019 4:03 PM
  • User387919 posted

    Hi @LandLu , What is the best place to run this code from ? I tried to wrap this code in a method and call it from FinishedLaunching override, in my AppDelegate, but UIApplication.SharedApplication.Windows is 0-lenght array. Am I calling it from the wrong place ?

    Thanks, Michal

    Monday, February 17, 2020 12:43 PM
  • User387919 posted

    Hi @LandLu , I was wrong - I called the code in my override, but before calling base.FinishedLaunching - what was the culprit of my problem. Your code works well. Thanks!!

    Monday, February 17, 2020 8:28 PM