Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>