2017-09-26 82 views
0

enter image description here使用Navigation渲染器我嘗試通過設置以下內容來更改工具欄的背景顏色。如何更改輔助工具欄的背景顏色 - xamarin ios

this.Toolbar.BackgroundColor = Color.Yellow; 

但我的輔助工具欄顏色沒有變化。

任何人都可以讓我知道如何更改Xamarin iOS中的輔助工具欄的背景顏色?

+0

這是你在做什麼尋找? https://developer.xamarin.com/recipes/ios/content_controls/navigation_controller/change_the_nav_bar_color/ – cvanbeek

+0

我正在討論輔助工具欄...您使用Order屬性啓用的工具欄: user3903423

+0

什麼是」工具欄「,是指自定義渲染器」NavigationRenderer「中的工具欄,還可以附加圖像來描述您的問題,我認爲它會更直觀 –

回答

0

我做到了,通過以下方式:

[assembly: ExportRenderer(typeof(NavigationPage), typeof(ExtendedNavigationRenderer))] 
namespace Sample.iOS 
{ 
    public class ExtendedNavigationRenderer : NavigationRenderer 
    { 
     UIToolbar _secondaryToolbar; 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      //_secondaryToolbar = View.Subviews.OfType<UIToolbar>().FirstOrDefault(v => v.GetType() != typeof(UIToolbar)); 
      _secondaryToolbar = View.Subviews.OfType<UIToolbar>().FirstOrDefault(); 
      if (_secondaryToolbar != null) 
       _secondaryToolbar.BarTintColor = this.NavigationBar.BarTintColor; 
     } 

     public override void ViewDidLayoutSubviews() 
     { 
      base.ViewDidLayoutSubviews(); 

      if (_secondaryToolbar != null && _secondaryToolbar.Items != null) 
      { 
       foreach (UIBarButtonItem item in _secondaryToolbar.Items) 
       { 
        var label = item.CustomView.Subviews.OfType<UILabel>().FirstOrDefault(); 
        if (label != null) 
        { 
         label.TextColor = UINavigationBar.Appearance.TitleTextAttributes.ForegroundColor; 
         //label.Font = label.Font.WithSize(12f); 
        } 
       } 
      } 
     } 

(TintColor做法帶來的真正的顏色,是從主導航欄略有不同,但NavigationBar.BackgroundColor爲null)

相關問題