c# - 在 iOS13 上更改状态栏颜色

标签 c# xamarin xamarin.ios ios13

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

        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 上,我收到以下运行时错误

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 上更改状态栏吗?

编辑:只是要指出,这是针对 Xamarin 而不是针对 Swift。澄清重复标记。

最佳答案

从错误中,你需要使用 UIStatusBarManager 在 IOS 13 中。

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

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

否则以下方法可以使其工作。

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

如果 View 没有完全渲染,UIApplication.SharedApplication.KeyWindow 将返回null。所以您可以在完全渲染后更改状态栏颜色。这里是 sample .

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

如果在 Forms 项目中,您可以尝试在 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 添加到项目中。

关于c# - 在 iOS13 上更改状态栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58069085/

相关文章:

ios - 静音本地通知 IOS

c# - 如何从 Unity 游戏对象列表中禁用子对象

c# - DataGridView 自动调整高度

用于 C 结构和函数的 C# 编码

c# - Xamarin 引用未声明

xamarin - 从 Visual Studio 2017 15.5.1 开始,如何创建 Xamarin.Forms 项目以使用 PCL 来定位最低 Android KitKat (API 19)?

c# - 在 Windows、MonoForAndroid 和 MonoTouch 上实现接口(interface)的差异

c# - 使用数据库中的值加载 DataGridView 中的指定列

.net - 添加对面向 .NET 标准 2.0 的类库的引用到面向 .NET 标准 4.5 的 Xamarin PCL

ios - "The native class hasn' t been loading"错误与我的绑定(bind)