Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a TabBarItem in Xamarin iOS

I have a Storyboard with a TabBarController and six subviews as Tabs. The TabBarController and each Tab has a Controller Class assigned. The content of some tabs is loaded from a server. In some cases there is no content for a tab and for this case I want to hide the TabBarItem.

I've tried (just for testing)

this.TabBarController.TabBar.Items [0].Enabled = false;

but it doesn't work.

How can I hide a single TabBarItems? I've searched Google, StackOverflow and the Xamarin Forum but found no solution for my problem. The only solution I've found was to remove the subview from the subviews-array, but in this case I cannot simply "reactivate" the tab if I want to show the tab again.

like image 235
Didatus Avatar asked Dec 16 '25 17:12

Didatus


2 Answers

In case of UITabBar:

UITabBar sampleTabBar;

public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // Perform any additional setup after loading the view, typically from a nib.

            sampleTabBar = new UITabBar ();
            sampleTabBar.Frame = new CoreGraphics.CGRect (10f, 64f, this.View.Frame.Width - 10, 50f);

            var tabBarItem = new UITabBarItem ("TAB BAR ITEM", null, 4);
            sampleTabBar.Items = new UITabBarItem[]{ tabBarItem };
            sampleTabBar.ItemSelected += (sender, e) => {
                Console.WriteLine ("tab bar button item slected");

             //Disable Tab Bar item here
                tabBarItem.Enabled = false;
                tabBarItem.Title = "Disabled now";
            };

            this.View.AddSubview (sampleTabBar);
        }

tabBarItem.Image property also can be used for getting the result.

1) Set a placeholder image as default.
2) Populate images from array/source to uibarButtonItem.
3) Use placeholder image wherever need to hide uibarbuttonitem.
like image 190
Alvin George Avatar answered Dec 19 '25 05:12

Alvin George


I think if you want to completely hide the UITabbarItem you will need to remove the UIViewController from the UITabbarController like so:

    var tbViewControllers = new List<UIViewController> (TabBarController.ViewControllers);
    tbViewControllers.RemoveAt (2); // remove whatever index you need.
    TabBarController.ViewControllers = tbViewControllers.ToArray ();

but you will need to keep a reference to all the UIViewControllers you want and add it back in like so:

    var tbViewControllers = new List<UIViewController> (TabBarController.ViewControllers);
    tbViewControllers.Insert (2, new RemvoedViewController());
    TabBarController.ViewControllers = tbViewControllers.ToArray ();
like image 44
Iain Smith Avatar answered Dec 19 '25 07:12

Iain Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!