Using iOS system icons in a NavigationPage with a TabbedPage

698 views Asked by At

I'm using a custom renderer to use iOS system icons in the navigation bar. It works fine, except that if the page is a TabbedPage, only the navigation icons for the default tab's page get their system icons. On other tabs, the system icons don't appear.

My current approach is to override PushViewController. The problem seems to be that when it's called, only the button items for that first tab are available. How can the custom renderer detect when the buttons on the navigation bar are changing? Or is there a better approach?

Current implementation:

/// <summary>
/// Sets system icons on the navigation bar that match the item text.
/// </summary>
class SystemIconNavigationRenderer : NavigationRenderer
{
    public override void PushViewController(UIViewController viewController, bool animated)
    {
        base.PushViewController(viewController, animated);

        // If any buttons are customized, replaces the list. Editing individual items doesn't work because UIBarButtonItem.Image is null for a new UIBarButtonItem created from a system item.
        var items = viewController.NavigationItem.RightBarButtonItems;
        bool changed = false;
        var newItems = new UIBarButtonItem[items.Length];
        for (int i = 0; i < items.Length; ++i) {
            var item = items[i];
            UIBarButtonSystemItem systemItem = (UIBarButtonSystemItem)(-1);
            switch (item.Title) {
                case nameof(UIBarButtonSystemItem.Add): systemItem = UIBarButtonSystemItem.Add; break;
                // More icons...
            }
            if (systemItem >= 0) {
                newItems[i] = new UIBarButtonItem(systemItem) { Action = item.Action, Target = item.Target };
                changed = true;
            } else
                newItems[i] = item;
        }
        if (changed) viewController.NavigationItem.RightBarButtonItems = newItems;
    }
}
0

There are 0 answers