Xamarin表单iOS渲染器 - 工具栏标准图标

时间:2016-07-27 13:49:53

标签: ios xamarin xamarin.forms renderer

我可以创建一个简单的Tab Renderer,它将更新我的Forms ToolBarItems以使用如下所示的内置iOS图标。

NavigationControllerNULL ViewWillAppear ViewDidLoad如果我在NULL中尝试,则为[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(); } } } }

问题在于,在用实际图标替换之前,你会看到一个TabBar Item文本的闪光。

我应该拦截ToolBar行为吗?

{{1}}

3 个答案:

答案 0 :(得分:1)

覆盖OnElementChanged方法:

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

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

            // Your code goes here
        }
    }

答案 1 :(得分:1)

是的,您应该覆盖PushViewController并继承NavigationRenderer方法。以下是我在申请中使用的内容:

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

答案 2 :(得分:1)

在最后或执行所需代码块后尝试以下代码......

return base.ViewWillAppear(animated);