在带有TabbedPage的NavigationPage中使用iOS系统图标

时间:2017-08-28 14:00:49

标签: xamarin.ios xamarin.forms uinavigationbar uitabbar custom-renderer

我使用自定义渲染器在导航栏中使用iOS system icons。它工作正常,但如果页面是TabbedPage,则只有默认选项卡页面的导航图标才能获得系统图标。在其他选项卡上,系统图标不会显示。

我目前的做法是覆盖PushViewController。问题似乎是,当它被调用时,只有第一个选项卡的按钮项可用。自定义渲染器如何检测导航栏上的按钮何时发生变化?或者有更好的方法吗?

目前的实施:

/// <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 个答案:

没有答案