Xamarion.iOS statusBar BackgroundColor 不会改变

Xamarion.iOS statusBar BackgroundColor won‘t change

自 iOS13 的暗模式以来,我正在努力在我的 Xamarin.Forms 应用程序中支持它。显示 NavigationBar 时,除 StatusBar 颜色外,一切正常。 Bar 的通常行为应该是根据 Navbar 的 BGColor 更改 backgroundColor,但由于某种原因它始终保持白色。 我试过像这样手动给它上色

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView; statusBar.BackgroundColor = UIColor.FromRGB(61, 205, 88);

但这并没有改变任何东西。 下面是它的外观截图

在Xamarin.Forms应用程序中,修改状态栏需要在iOS解决方案中编辑Info.plist

首先,添加UIViewControllerBasedStatusBarAppearance到文件:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
//Follow can change text color of status bar
//<key>UIStatusBarStyle</key>
//<string>UIStatusBarStyleDarkContent</string>

第二个,在Xamarin.iOS项目中,可以修改AppDelegate.cs中的statsbar背景如下:

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;
        statusBar.TintColor = UIColor.White;
        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;
        }
    }
    base.OnActivated(uiApplication);
}

你也可以看看 this discussion .