Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms iOS Renderer - ToolBar Standard Icons

I can create a simple Tab Renderer which will update my Forms ToolBarItems to use the built in iOS icons like below.

NavigationController is only NOT NULL in ViewWillAppear If I try it in ViewDidLoad, it is NULL.

The problem with this is you get a flash of the TabBar Item text before it gets replaced with the actual icon.

Is there a different place I should be intercepting the ToolBar behavior?

[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabRenderer))]
namespace Cellar.iOS.Renders
{
    public class TabRenderer : TabbedRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            var list = new List<UIBarButtonItem>();

            foreach (var item in NavigationController.TopViewController.NavigationItem.RightBarButtonItems)
            {
                if (string.IsNullOrEmpty(item.Title))
                {
                    continue;
                }

                if (item.Title.ToLower() == "add")
                {
                    var newItem = new UIBarButtonItem(UIBarButtonSystemItem.Add)
                    {
                        Action = item.Action,
                        Target = item.Target
                    };

                    list.Add(newItem);
                }

                if (list.Count > 0)
                    NavigationController.TopViewController.NavigationItem.RightBarButtonItems = list.ToArray();
            }
        }
    }
}
like image 348
aherrick Avatar asked Jul 27 '16 13:07

aherrick


1 Answers

override OnElementChanged method:

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if(e.NewElement!= null)
        {
            var list = new List<UIBarButtonItem>();

            // Your code goes here
        }
    }
like image 128
eakgul Avatar answered Oct 21 '22 04:10

eakgul