在 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/