Change status bar colour on iOS13

前端 未结 3 1647
遥遥无期
遥遥无期 2021-01-14 04:47

Before iOS 13 I could change the status bar colour using the following bit of code:

        UIView statusBar = UIApplication.SharedApplication.ValueForKey(ne         


        
3条回答
  •  心在旅途
    2021-01-14 05:26

    From error , you need to use UIStatusBarManager in IOS 13.

    If you have updated VS to the latest version(Visual Studio 2019 version 16.3.0/Visual Studio 2019 for Mac version 8.3 above), you can change color as follow:

    UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
    statusBar.BackgroundColor = UIColor.Yellow;
    UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
    

    Else the follow methods can make it works .

    UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
    statusBar.BackgroundColor = UIColor.Yellow;
    UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
    

    If the view is not fully rendered , UIApplication.SharedApplication.KeyWindow will return null .So you can change status bar color after fully rendered . Here is the sample .

    ===================================Update=================================

    If in Forms project , you can have a try with invoking method in AppDelegate.cs

    public override void OnActivated(UIApplication uiApplication)
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
        {
            // 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;
            UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
        }
        else
        {
            UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
            if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
            {
                statusBar.BackgroundColor = UIColor.Red;
                UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
            }
        }
        base.OnActivated(uiApplication);
    } 
    

    Note :

    Not sure this solurion will always work in AppDelegate.cs, better invoked in Controller.cs's method , because from iOS 13 , Apple have modified the architure of AppDelegate and added SceneDelegate to project.

提交回复
热议问题