无法在Xamarin.iOS中设置状态栏背景

时间:2019-02-25 07:24:44

标签: xamarin.ios mono

我需要在Xamarin.iOS(非表单)中更改状态栏UI,其中背景为黑色,文本颜色为白色。 为此,我想自定义状态栏视图,在Internet上进行了搜索,发现下面的代码适用于大多数人。

var statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
//tried with key statusBarWindow.statusBar as well.
statusBar.BackgroundColor = ...;

以上代码在Xamarin.iOS中崩溃(在iPhone Xs Simulator上测试)。 这是崩溃的日志。

在(包装本机托管)ObjCRuntime.Messaging.xamarin_IntPtr_objc_msgSend_IntPtr(intptr,intptr,intptr)   在/Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.12/src/Xamarin.iOS/Foundation/NSObject.g.cs:599的Foundation.NSObject.ValueForKey(Foundation.NSString键)[0x0001c]中>

任何人都可以向我提供有关为什么此代码在Xamarin中崩溃的任何提示。 我试图在此GitHub线程https://github.com/mono/Embeddinator-4000/issues/597

上找到原因

但是在这里我也无法获得任何线索。 另外,如果还有其他自定义状态栏的方法,我想放弃这个想法并继续提出建议。

Refer here

1 个答案:

答案 0 :(得分:0)

据我所知,这是行得通的,但是通常当我使用它时,我总是使用locals: {parent: $scope} 检查选择器是否可用:

RespondsToSelector

此外,不要忘记将样式添加到info.plist

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
    statusBar.BackgroundColor = UIColor.White;
}

更新

<key>UIViewControllerBasedStatusBarAppearance</key> <false/> <key>UIStatusBarStyle</key> <string>UIStatusBarStyleBlackOpaque</string> 方法的AppDelegate.cs中使用以上代码。