首页 > 解决方案 > Xamarin.Forms NavigationBar 颜色自 iOS 4.6 以来被覆盖

问题描述

由于我已更新到 Xamarin.Forms 4.6,我的 NavigationBar 颜色会被我无法弄清楚的任何来源覆盖。

我正在根据当前主题(暗/亮)设置 BarTintColor,如下所示:

this.NavigationController.NavigationBar.BarTintColor = Color.FromHex("#212121").ToUIColor();

但它不断被纯黑色或灰色(取决于深色/浅色)覆盖。我也尝试通过设置它UINavigationBar.Appearance.BarTintColor,也没有改变。此外,我正在设置 TintColor(条形的字体颜色),如下所示:

this.NavigationBar.TintColor = UIColor.FromRGB(38, 100, 137);

当我启动应用程序时它工作正常,但是一旦我在我的应用程序中导航到其他地方,它就会变回默认的系统蓝色。

标签: iosxamarinxamarin.formsxamarin.ios

解决方案


在 Xamarin Forms 中,有一种直接的方法可以修改NavigationBarin的颜色App.xaml.cs

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage())
    {
        BackgroundColor = Color.Yellow,
        BarTextColor = Color.Black
    };
}

效果:

在此处输入图像描述

如果需要修改状态栏的颜色,可以编写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.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.White;
            UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
        }
    }
    base.OnActivated(uiApplication);
}

效果:

在此处输入图像描述


推荐阅读