locked
How to access “tabBarController: shouldSelectViewController:” on Xamarin.Forms? RRS feed

  • Question

  • User176755 posted

    I need to have control of this method so that I can make a change in my app. But I couldn't make this implementation work, can anyone help?

    Here is the Custom Renderer of my TabbedPage:

    public class MainTabbedPageRenderer : TabbedRenderer, IUITabBarControllerDelegate
    {
         [Export("tabBarController:shouldSelectViewController:")]
         public bool ShouldSelectViewController(UITabBarController tabBarController, UIViewController viewController)
         {
              return false;
         }
    }
    

    The breakpoint does not stop there at all.

    I have the impression that it does not stop at breakpoint because TabBarController is always null, but the screen loads and performs navigations normally, I also could not make this TabBarController be filled.

    You can click on tabbar items using this method:

    [Export("tabBar:didSelectItem:")]
    public void ItemSelected(UITabBar tabbar, UITabBarItem item)
    {
    }
    
    Monday, October 21, 2019 10:31 PM

Answers

  • User369979 posted

    UITabBarController has a property called ShouldSelectViewController. You need to handle it directly instead of using delegate:

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
    
        if (ViewController != null)
        {
            ShouldSelectViewController += (tabBarController, viewController) =>
            {
                return false;
            };
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 22, 2019 6:53 AM

All replies

  • User369979 posted

    UITabBarController has a property called ShouldSelectViewController. You need to handle it directly instead of using delegate:

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
    
        if (ViewController != null)
        {
            ShouldSelectViewController += (tabBarController, viewController) =>
            {
                return false;
            };
        }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 22, 2019 6:53 AM
  • User176755 posted

    Thanks @LandLu !!!!!

    Tuesday, October 22, 2019 12:30 PM