Xamarin形成更改导航栏的背景颜色

我正在使用Xamarin.Forms并尝试更改iOS上导航栏的背景颜色。

我有一个inheritance自NavigationPage的自定义导航栏类​​,带有可绑定属性和构造函数,用于设置导航栏的颜色。 根据我的理解,导航栏上面有一个默认的背景(黑色)Xamarin.Forms导航背景。 我可以使用SetColor()方法设置背景颜色(见下文)。 然而,它留下了一条黑线,这是导航栏(iOS)的背景,如图所示。 图片链接

现在,我正在尝试将iOS导航栏背景颜色设置为白色或透明。 我花了很多时间但没有任何效果。 有人可以协助如何将背景设置为白色。

//PCL class public class CustomNavigationalPage : NavigationPage { public static readonly BindableProperty BarBgColorProperty = BindableProperty. Create (p => p.BarBackgroundColorR, null); public UIColor BarBackgroundColorR { get { return (UIColor)base.GetValue (BarBgColorProperty); } set { base.SetValue (BarBgColorProperty, value); } } public NavigationalPageCustomized() : base() { SetColor(); } void SetColor() { BarBackgroundColor = Color.Transparent; BarTextColor = Color.Blue; } } 

导航栏渲染器类:

 [assembly: ExportRenderer (typeof (CustomNavigationalPage), typeof (CustomNavigationPageRenderer))] namespace project.iOS { public class CustomNavigationPageRenderer : NavigationRenderer { public CustomNavigationPageRenderer() { // UINavigationBar.Appearance.SetBackgroundImage (UIImage.FromFile ("navbg.png"), UIBarMetrics.Default); } protected override void OnElementChanged (VisualElementChangedEventArgs args) { base.OnElementChanged (args); var nb = (NavigationalPageCustomized) Element; if (nb != null) { nb.BarBackgroundColorR = UIColor.White; } } } } 

Xamarin.formsPCL中试用此代码 在App.xaml.cs的构造函数中更改以下代码

 public App() { MainPage = new NavigationPage(new Page1()) { BarBackgroundColor = Color.Gray }; } 

请尝试以下代码。 祝好运

 [assembly: ExportRenderer (typeof (CustomNavigationalPage), typeof (CustomNavigationPageRenderer))] namespace project.iOS { public class CustomNavigationPageRenderer : NavigationRenderer { public CustomNavigationPageRenderer() { } public override void ViewDidLoad () { base.ViewDidLoad (); //Background image this.NavigationBar.BarTintColor = UIColor.FromPatternImage (UIImage.FromFile ("AnyResourceImage.png")); //Your desire color this.NavigationBar.BarTintColor = UIColor.Red; //Right item color this.NavigationBar.TopItem.RightBarButtonItem.TintColor = UIColor.FromPatternImage (UIImage.FromFile ("AnyResourceImage.png")); //Left item color this.NavigationBar.TopItem.LeftBarButtonItem.TintColor = UIColor.Black; } } } //Note : Please remove any background color you set in forms shared or pcl project. Hint in this class > CustomNavigationalPage 

这曾经需要自定义渲染器,但在XF 1.3中不再需要。 NavigationPage现在具有BarBackgroundColor和BarTextColor属性,这些属性似乎运行良好。 不幸的是,没有自定义渲染器(我已经找到),没有能力更改字体。

您可以在全局App.xaml文件中进行设置

  

改变你自己的颜色

对我来说这很有效:

 (App.Current.MainPage as NavigationPage).BarBackgroundColor = Color.FromHex("#4388CC"); 

我已经在页面的ViewModel的构造函数中使用了这段代码。

希望这也适合你。

NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes(){ForegroundColor = UIColor.White};