在iOS13上更改状态栏颜色 [英] Change status bar colour on iOS13

查看:75
本文介绍了在iOS13上更改状态栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 13之前,我可以使用以下代码更改状态栏的颜色:

  UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar"))作为UIView;如果(statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:"))){statusBar.BackgroundColor = UIColor.Clear.FromHex(0x323232);statusBar.TintColor = UIColor.White;app.StatusBarStyle = UIStatusBarStyle.BlackOpaque;} 

但是,在iOS13上,我收到以下运行时错误

抛出

Objective-C异常.名称:NSInternalInconsistencyException原因:在UIApplication上名为-statusBar或-statusBarWindow的应用程序:必须更改此代码,因为不再有状态栏或状态栏窗口.而是在窗口场景上使用statusBarManager对象.

关于如何在iOS13上更改状态栏的任何想法?

只是要指出,这是针对Xamarin而不是Swift.为了澄清重复的标记.

解决方案

由于错误,您需要使用 示例.

==================================更新=================================

如果在Forms项目中,您可以在 AppDelegate.cs

中尝试调用方法

 公共重写void OnActivated(UIApplication uiApplication){如果(UIDevice.CurrentDevice.CheckSystemVersion(13,0)){//如果VS已更新到最新版本,则可以使用StatusBarManager,否则使用第一行代码//UIView statusBar =新的UIView(UIApplication.SharedApplication.StatusBarFrame);UIView statusBar =新的UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);statusBar.BackgroundColor = UIColor.Red;UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);}别的{UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar"))作为UIView;如果(statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))){statusBar.BackgroundColor = UIColor.Red;UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;}}base.OnActivated(uiApplication);} 

注意:

不确定这种解决方案将始终在 AppDelegate.cs 中使用,最好在 Controller.cs 的方法中调用,因为从iOS 13开始,Apple修改了 AppDelegate ,并将 SceneDelegate 添加到项目中.

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

        UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
        if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
        {
            statusBar.BackgroundColor = UIColor.Clear.FromHex(0x323232);
            statusBar.TintColor = UIColor.White;
            app.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
        }

However, on iOS13 I get the following runtime error

Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.

Any idea on how to change the status bar on iOS13?

EDIT: Just to point out, this is for Xamarin and not for Swift. To clarify the duplicate marker.

解决方案

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.

这篇关于在iOS13上更改状态栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆