【问题标题】:Xamarin.Forms 5.0 NavBar BackgroudColor not changing on IOSXamarin.Forms 5.0 NavBar BackgroudColor 在 IOS 上没有改变
【发布时间】:2021-04-19 11:58:00
【问题描述】:

我在AppDelegate.cs 函数FinishedLaunching 中有这段代码:

 UINavigationBar.Appearance.BackgroundColor= Color.FromHex("07987f").ToUIColor(); 
 UINavigationBar.Appearance.BarTintColor = Color.FromHex("07987f").ToUIColor();
 UINavigationBar.Appearance.TintColor = Color.White.ToUIColor();
 UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes { ForegroundColor = UIColor.White };

在将 Xamarin.Forms 4.8 更新为 5.0 之前,此代码适用于每个导航栏,但现在只显示白色。 TintColorTextColor 工作正常。应该是什么问题? 图片:

【问题讨论】:

    标签: c# xamarin.forms colors xamarin.ios navbar


    【解决方案1】:

    您可以在创建 NavigationPage 时在 Xamarin.forms 中设置颜色:

    MainPage = new NavigationPage(new MainPage()) { 
        BackgroundColor = Color.FromHex("07987f"), 
        BarBackgroundColor = Color.FromHex("07987f") 
    };
    

    我也在 Github 上看到了一个关于这个问题的帖子,你可以在那里等待回复:

    UINavigationBar BackgroundColor can no longer be changed

    【讨论】:

    • 我添加了一张图片。它可能是 Xamarin 的最后一个版本。我会每天检查这篇文章。如果得到修复,我会写作为答案
    • 是的,我也可以在最新版本中重现该问题。您可以尝试使用我的代码作为解决方法。
    • 但是我必须在每个页面上添加您的代码
    • 不是,是在创建 NavigationPage 时在 App.cs 中设置的。
    • 好的,等待Github的回复。
    【解决方案2】:

    您应该尝试使用 NavigationPage 类通过 Xamarin.Forms 指定它。

    可以肯定,某些​​代码会在您的代码之后应用颜色,因此您需要禁用它(如果可能)或在后续行中覆盖它(如上所述)。

    【讨论】:

    • 你有样品吗?
    【解决方案3】:

    对于 iOS 15,导航栏颜色存在一个已知问题。您可以通过在 AppDeligate.cs 文件中设置颜色来覆盖默认行为。这将影响每个页面:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        //A bug with iOS 15 results in the backgorund color not being set on the main appshell nav so we need to force it here
        var appearance = new UINavigationBarAppearance()
        {
             BackgroundColor = UIColor.FromRGB(144, 191, 110),
             ShadowColor = UIColor.FromRGB(144, 191, 110),
        };
    
        UINavigationBar.Appearance.StandardAppearance = appearance;
        UINavigationBar.Appearance.ScrollEdgeAppearance = appearance;
    }
            
    

    StandardAppearance 和 ScrollEdgeAppearence 必须设置相同。更多信息:https://developer.apple.com/forums/thread/682420

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-27
      • 2018-04-03
      • 1970-01-01
      • 2021-03-04
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多