2

I have a tab bar with 5 tabs on it in my app. The final tab shows some ads. I want to add a setting where a user can turn that 5th tab "off".... so essentially I just remove it from the screen.

Note that I don't want to HIDE it, I want to REMOVE it, so that the 4 remaining tabs are evenly spaced automatically.

I want to do the same with ADDING the tab back in.

Is it possible to do this without the user having to restart the app?

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

1 Answers1

7

You just need to use UITabBarController's viewControllers property.

Use this code to remove the last view controller:

NSMutableArray *mutableViewControllers = [tabBarController.viewControllers mutableCopy];
[mutableViewControllers removeLastObject];
tabBarController.viewControllers = mutableViewControllers;

Use this code to restore the last view controller when needed:

NSMutableArray *mutableViewControllers = [tabBarController.viewControllers mutableCopy];
[mutableViewControllers addObject:previouslyRemovedViewController];
tabBarController.viewControllers = mutableViewControllers;

Of course this example assumes that you have a reference to tabBarController, and that you keep your lastViewController (on a property for example).

Also, make sure to run this code on the main thread.

robbartoszewski
  • 896
  • 6
  • 11