How to set a custom right button on the navigation tool bar?

I'm trying to add a coloured icon as a right button to the navigation bar (On iOS), which I managed to do, but I'm not quite sure if it's the proper way to do so.

Here is my solution: I created a new NavigationRenderer and overrided the ViewDidLayoutSubviews method to add a new UIButton to NavigationBar.TopItem.RightBarButtonItem like so:

public override void ViewDidLayoutSubviews()
        {
            base.ViewDidLayoutSubviews();

            UIButton DBButton = new UIButton(UIButtonType.Custom);

            var tempImg = UIImage.FromBundle("righticon");
            DBButton.SetImage(tempImg, UIControlState.Normal);

            DBButton.Frame = new RectangleF(0, 0, (float)tempImg.Size.Width, (float)tempImg.Size.Height);

            NavigationBar.TopItem.RightBarButtonItem = new UIBarButtonItem(DBButton);
        }

This way the right button shows on every page. (I tried doing this on ViewDidLoad but the icon only showed on root)
But if I navigate to another page the button stay is place while the rest of the navigation bar items do an animation which makes me believe my button isn't in the right place. Or is this correct an all I need is to add an animation in the Draw method of the UIButton?

Answers

  • NickKovalskyNickKovalsky USMember ✭✭

    Nice renderer. And why not just use a toolbar icon on every page?

  • NicolasClementNicolasClement CHMember ✭✭

    Are you referencing to the list ToolbarItems? If so I've already tried it but my icon comes out coloured in the BarTextColor of the page.

Sign In or Register to comment.