Custom renderer for Toolbar Items not working for TabbedPage

UmairEmUmairEm GBMember ✭✭

Hi,

I have 2 toolbar items in tabbed page (with two tabs).

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage 
        xmlns="site wont let me post links"
        xmlns:x="hence deal with custom qualified names"
        xmlns:views="clr-namespace:TestApp.Views"
        x:Class="TestApp.Views.MyTabbedPage"
        >
    <views:Page1 Title="Page 1" />
    <views:Page 2 Title="Page 2" />
</TabbedPage>

I am trying to move one toolbar item on left using custom renderer:

assembly: ExportRenderer(typeof(MyTabbedPage), typeof(TabbedPageRenderer))]
namespace TestApp.iOS
{
    public class TabbedPageRenderer : TabbedRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            var navigationItem = this.NavigationController.TopViewController.NavigationItem;
            if (navigationItem.RightBarButtonItems.Length > 1)
            {
                navigationItem.LeftBarButtonItems = new UIBarButtonItem[] { navigationItem.RightBarButtonItems[1] };
            }            
            navigationItem.RightBarButtonItems = new UIBarButtonItem[] { navigationItem.RightBarButtonItems[0]};
        }

    }
}

it works fine when page is loaded for first time. But when I click on tab to show the Page 2, It renders both toolbar items on right again (item on left stays at left) resulting in 3 toolbar items.

Can anyone please help me with this small issue?

Cheers

Answers

  • MohammadShabanMohammadShaban USMember ✭✭

    +1
    same here
    could you resolved it?

  • UmairEmUmairEm GBMember ✭✭

    I couldn't figure out the way I was trying to. But as I am writing a custom renderer anyway I have added the left button manually:

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
    
        var navigationItem = this.NavigationController.TopViewController.NavigationItem;
    
        UIBarButtonItem leftButton = new UIBarButtonItem();
        leftButton.Title = "Leftie";
        leftButton.Clicked += OnLeftbuttonClicked;
        navigationItem.LeftBarButtonItems = new UIBarButtonItem[] { leftButton };
    }
    
    private void OnLeftbuttonClicked(object sender, EventArgs e)
    {
        var page = Element as MyTabbedPage;
        if (page != null)
        {
          page.OnLeftButtonClicked(sender, e);  // add OnLeftbuttonClicked method in xaml.cs file for page to handle event
        }
    }
    
Sign In or Register to comment.