在iOS 13之前,我可以使用以下代码更改状态栏的颜色:
SessionFactory
但是,在iOS13上,我收到以下运行时错误
Objective-C异常抛出。名称:NSInternalInconsistencyException原因:在UIApplication上名为-statusBar或-statusBarWindow的应用程序:必须更改此代码,因为不再有状态栏或状态栏窗口。而是在窗口场景上使用statusBarManager对象。
关于如何在iOS13上更改状态栏的任何想法?
编辑:只是要指出,这是针对Xamarin而不是Swift。为了澄清重复的标记。
答案 0 :(得分:2)
由于错误,您需要在IOS 13中使用 UIStatusBarManager 。
如果,您已将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);
如果未完全渲染视图,则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 进行投影。
答案 1 :(得分:0)
如果在UIViewController中的ViewDidAppear重写中使用,这也将起作用。我以前在ViewWillAppear中进行了测试,甚至还为时过早,因此KeyWindow不为空:
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;
}
}
}
中提供的解决方案相同
答案 2 :(得分:-1)
请尝试以下解决方案。在相同情况下对我来说效果很好
if (@available(iOS 13.0, *)) {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];
statusBar.backgroundColor = [UIColor redColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
// Fallback on earlier versions
UIView *statusBar=[[UIApplication sharedApplication] valueForKey:@"statusBar"];
statusBar.backgroundColor = [UIColor redColor];
[statusBar setNeedsDisplay];
}