Change status bar colour on iOS13

前端 未结 3 1643
遥遥无期
遥遥无期 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:23

    This also works, if used in ViewDidAppear override in a UIViewController. I previously tested in ViewWillAppear and even that was too soon to have a KeyWindow non-null:

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
        {
            //Obj-C: 
            // UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
            // statusBar.backgroundColor = [UIColor redColor];
            // [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
    
            // Xamarin.iOS: 
            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;
                statusBar.TintColor = UIColor.White;
                UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
            }
        }
    }
    

    And that is the same solution provided in the duplicate question: https://stackoverflow.com/a/58028658/2913599

提交回复
热议问题