簡體   English   中英

Xamarin形成更改導航欄的背景顏色

[英]Xamarin forms change background color of navigation bar

我正在使用Xamarin.Forms並嘗試更改iOS上導航欄的背景顏色。

我有一個繼承自NavigationPage的自定義導航欄類​​,帶有可綁定屬性和構造函數,用於設置導航欄的顏色。 根據我的理解,導航欄上面有一個默認的背景(黑色)Xamarin.Forms導航背景。 我可以使用SetColor()方法設置背景顏色(見下文)。 然而,它留下了一條黑線,這是導航欄(iOS)的背景,如圖所示。 圖片鏈接

現在,我正在嘗試將iOS導航欄背景顏色設置為白色或透明。 我花了很多時間但沒有任何效果。 有人可以協助如何將背景設置為白色。

//PCL class
public class CustomNavigationalPage : NavigationPage
{
    public  static readonly BindableProperty BarBgColorProperty = 
        BindableProperty.
        Create<CustomNavigationalPage, UIColor>  
            (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

您可以在全局App.xaml文件中進行設置

<Style TargetType="NavigationPage">
       <Setter Property="BarBackgroundColor" Value="Blue"/>
       <Setter Property="BarTextColor" Value="White"/>
</Style>

改變你自己的顏色

這曾經需要自定義渲染器,但在XF 1.3中不再需要。 NavigationPage現在具有BarBackgroundColor和BarTextColor屬性,這些屬性似乎運行良好。 不幸的是,沒有自定義渲染器(我已經找到),沒有能力更改字體。

對我來說這很有效:

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

我已經在頁面的ViewModel的構造函數中使用了這段代碼。

希望這也適合你。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM