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

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

问题描述

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

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;
        }

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

However, on iOS13 I get the following runtime error

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

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.

知道如何更改 iOS13 上的状态栏吗?

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

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

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

推荐答案

从错误开始,需要使用 UIStatusBarManager.

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

如果您已将 VS 更新到最新版本(Visual Studio 2019 版本 16.3.0/Visual Studio 2019 for Mac 版本 8.3 以上),您可以按如下方式更改颜色:

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);

如果视图未完全渲染,UIApplication.SharedApplication.KeyWindow 将返回 null .因此您可以在完全渲染后更改状态栏颜色.这是示例.

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=================================

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

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);
} 

注意:

不确定这个解决方案在 AppDelegate.cs 中是否总是有效,最好在 Controller.cs 的方法中调用,因为从 iOS 13 开始,Apple 已经修改了AppDelegate 并将 SceneDelegate 添加到项目中.

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天全站免登陆