aherrick
aherrick

Reputation: 20161

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();
            }
        }
    }
}

Upvotes: 11

Views: 1151

Answers (3)

Senthamizh
Senthamizh

Reputation: 281

Try the below code at the end or after the execution of the required code block...

return base.ViewWillAppear(animated);

Upvotes: 1

valdetero
valdetero

Reputation: 4652

Yes, you should override the PushViewController and inherit from NavigationRenderer method. Here is what I am using in my application:

public class CustomToolbarRenderer :  NavigationRenderer
{
    public override void PushViewController(UIViewController viewController, bool animated)
    {
        base.PushViewController(viewController, animated);

        List<UIBarButtonItem> newItems = new List<UIBarButtonItem>();

        foreach (UIBarButtonItem i in TopViewController.NavigationItem.RightBarButtonItems)
        {
            if (i.Title != null)
            {
                if (i.Title.Equals(Constants.Toolbar.Add))
                {
                    var newItem = new UIBarButtonItem(UIBarButtonSystemItem.Add);
                    newItem.Action = i.Action;
                    newItem.Target = i.Target;

                    newItems.Add(newItem);
                }
                else if (i.Title.Equals(Constants.Toolbar.Camera))
                {
                    var newItem = new UIBarButtonItem(UIBarButtonSystemItem.Camera);
                    newItem.Action = i.Action;
                    newItem.Target = i.Target;

                    newItems.Add(newItem);
                }
                else if (i.Title.Equals(Constants.Toolbar.Delete))
                {
                    var newItem = new UIBarButtonItem(UIBarButtonSystemItem.Trash);
                    newItem.Action = i.Action;
                    newItem.Target = i.Target;

                    newItems.Add(newItem);
                }
                else
                    newItems.Add(i);
            }
            else
                newItems.Add(i);
        }

        TopViewController.NavigationItem.RightBarButtonItems = newItems.ToArray();
    }
}

Upvotes: 1

eakgul
eakgul

Reputation: 3698

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
        }
    }

Upvotes: 1

Related Questions