Cancel ItemSelected Event in TabbarRenderer Xamarin.iOS

I have a custom tabbar renderer for iOS in a Xamarin.Forms app. In certain cases, I need to stop the ItemSelected event. Essentially, if the user taps the item on the tabbar, I need to perform a custom action and NOT navigate.

It seems that the ItemSelectedevent is too late to do this. I know that there is a ShouldShowViewController method on the UITabbarController, but this has been abstracted away but the TabbarRenderer.

How can I achieve this?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited November 2018
    [assembly:ExportRenderer(typeof(TabbedPage),typeof(MyTabRenderer))]
    namespace FormsApp.iOS
    {
        class MyTabRenderer : TabbedRenderer
        {public override void ViewDidLoad()
            {
                base.ViewDidLoad();
    
                this.ShouldSelectViewController += (tabBarController, viewController) =>
                {
                    //perform a custom action
                    return false;
                };
            }
        }
    }
    
  • LuizNegriniLuizNegrini USMember ✭✭

    @ColeX can we make this on Xamarin.Forms??

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited October 2019

    That is Xamarin.Forms. This is the same thing as I showed you on stack overflow, just where it is is different, IOW I had you do:

    public class MainTabbedPageRenderer : TabbedRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
    
            if (e.NewElement != null)
              this.ShouldSelectViewController = ShouldSelectViewControllerHandler;
        }
    
        bool ShouldSelectViewControllerHandler(UITabBarController tabBarController, UIViewController viewController)
        {
            return false;
        }
    }
    

    End result is identical though.

Sign In or Register to comment.