【问题标题】:Xamarin.Forms NavigationBar Colors overwritten since 4.6 on iOSXamarin.Forms NavigationBar 颜色自 iOS 4.6 以来被覆盖
【发布时间】:2020-05-12 14:42:17
【问题描述】:

自从我更新到 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);

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

【问题讨论】:

  • 你确定它从 4.6 开始就出现了吗?另外,既然您使用的是表单,为什么不将其设置在共享项目中?

标签: ios xamarin xamarin.forms xamarin.ios


【解决方案1】:

在 Xamarin Forms 中,可以直接修改 App.xaml.csNavigationBar 的颜色。

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);
}

效果:

【讨论】:

  • @EliasJohannes Okey,你能在这里分享示例项目链接吗?我会在我的本地网站上查看。
猜你喜欢
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 2020-01-20
相关资源
最近更新 更多