如何在导航栏中更改标题的位置

时间:2016-11-15 08:56:42

标签: xamarin.forms

如何以xamarin形式更改导航栏中标题的位置,从左侧的默认位置到中间或右侧。

1 个答案:

答案 0 :(得分:0)

You will need a custom render, here's how i have done it in IOS: (the thing is no way to you to get info from tamarin forms for the UIToolbarItem, so you can use the title to get the button u want or hardcode.

(this code, shows moving the 1st button to the left, leaving the other 2 buttons on the right) attached a screenshot

Create a custom PageRenderer to the page type where you want to access,

[assembly: ExportRenderer(typeof(CategoriesPage), typeof(CategoriesPageRenderer))]

and add

    bool updatedButtons = false;
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear (animated);

            if (!updatedButtons)
                MoveButtonToLeft ();
        }


        protected void MoveButtonToLeft()
        {

            var infoButton = NavigationController.TopViewController.NavigationItem.RightBarButtonItems[0];
            NavigationController.TopViewController.NavigationItem.LeftBarButtonItem = infoButton;

            var favButton = NavigationController.TopViewController.NavigationItem.RightBarButtonItems[1];
            var musicButton = NavigationController.TopViewController.NavigationItem.RightBarButtonItems[2];
            NavigationController.TopViewController.NavigationItem.RightBarButtonItems = new UIBarButtonItem[2] {favButton, musicButton };

            updatedButtons = true;
        }