1
votes

enter image description hereUsing Navigation renderer I tried changing the background color of toolbar by setting the following.

this.Toolbar.BackgroundColor = Color.Yellow;

But my secondary toolbar color is not getting changed.

Can anyone let me know how to change the background color of secondary tool bar in xamarin iOS ?

1
I'm talking about the secondary toolbar...The one that you enable with the Order attribute: <ToolbarItem Icon="icon1.png"></ToolbarItem> <ToolbarItem Icon="icon2.png"></ToolbarItem> <ToolbarItem Icon="icon3.png" Order="Secondary" Text="Task3"></ToolbarItem> <ToolbarItem Icon="icon4.png" Order="Secondary" Text="Task4"></ToolbarItem>Ponni Chandran
what is Toolbar, do you mean the toolbar in custom renderer NavigationRenderer , and also could you attach the image to describe your issue, I think it will be more intuitiveColeX - MSFT
@ColeXia While adding toolbar items if the order is specified to be secondary..the items are added in a secondary tool bar below the navigationbar .. Thanks ..I have added the image also for reference.Ponni Chandran
have you ever found an answer for this? please let me know if you didEmil

1 Answers

0
votes

I did it in the following way:

[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 approach brings the real color that is slightly different from primary NavigationBar, but NavigationBar.BackgroundColor is null)