Xamarin IOS : StatusBar BackgroundColor not changing on IOS 14

Mohammed Salloum 26 Reputation points
2021-04-22T22:42:27.167+00:00

Hey, I can't change status bar color in IOS 14.
I use Xamarin IOS Platform "not Form"

        [Export ("application:didFinishLaunchingWithOptions:")]
        public bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
        {
            // Override point for customization after application launch.
            // If not required for your application you can safely delete this method
            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                UIWindow Window = new UIWindow(UIScreen.MainScreen.Bounds)
                {
                    RootViewController = new UIViewController()
                };
                Window.MakeKeyAndVisible();
                // If VS has updated to the latest version , you can use StatusBarManager , else use the first line code
                UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
                //   UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
                statusBar.BackgroundColor = UIColor.Red;
                statusBar.TintColor = UIColor.Orange;
                //UIApplication.SharedApplication.Delegate.GetWindow().AddSubview(statusBar);
                UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
            }
            else
            {
                //  UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
                UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBarWindow")).ValueForKey(new NSString("statusBar")) as UIView;
                if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
                {
                    statusBar.BackgroundColor = UIColor.Red;
                    statusBar.TintColor = UIColor.Yellow;
                    UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
                }
            }
            return true;
        }

best regards,
Mohammed Salloum

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-04-23T04:45:39.147+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I tried the code on iOS14 in Xamarin.iOS and even in native iOS , it does not work .

    KeyWindow is deprecated on iOS 13, we have to use another complicated way to get it ,refer to here .

    However , I test the code (even test it in viewcontroller, SceneDelegate )it does not work in any scenario.

    I'm afraid Apple does not want us to change the system theme directly, the only way is to let it change with Navigation bar style,refer to here .


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Mohammed Salloum 26 Reputation points
    2021-04-23T18:01:33.04+00:00

    Thank you for the answer
    Now it works!
    Here is the code if someone needs it

            private void SetStatusBarColor()
            {
                UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
                statusBar.BackgroundColor = UIColor.Red;
                statusBar.TintColor = UIColor.Orange;
                foreach (UIScene scene in UIApplication.SharedApplication.ConnectedScenes)
                {
                    if (scene.ActivationState == UISceneActivationState.ForegroundActive)
                    {
                        UIWindowScene myScene = (UIWindowScene)scene;
                        foreach (UIWindow win in myScene.Windows)
                        {
                            if (win.IsKeyWindow)
                            {
                                win.AddSubview(statusBar);
                            }
                        }
    
                    }
                }
            }
    

    best regards,
    Mohammed Salloum

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.