1

I am using Xcode 10.1 and Swift 4.2.1. I have Tab Bar app like this:

enter image description here

For some reasons I want to hide/delete one of the bars to receive something like this:

enter image description here

So, the question is: how to hide one Tab of Tab Bar? To specify: some button pressed, one tab hides. Another press and Tab appears again.

Alex
  • 1,038
  • 2
  • 12
  • 32

1 Answers1

5

To remove:

tabBarController?.viewControllers?.remove(at: index)

To add back:

tabBarController?.viewControllers?.insert(newElement: viewController, at: index)
ZN123
  • 122
  • 11
  • Thank you very much! It's working perfectly: fast, no crash and you can even hide your own view! But can I expand my question a little bit? How to do this trick from AppDelegate? – Alex Dec 15 '18 at 17:45
  • 1
    `let window = UIWindow(frame: UIScreen.main.bounds) let tabBarController = window?.rootViewController as! UITabBarController` – ZN123 Dec 17 '18 at 09:49
  • Thank for your answer! But in method func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { it cause crash. – Alex Dec 17 '18 at 13:24
  • 1
    Better do like this: if let rootViewController = window?.rootViewController as? UITabBarController { rootViewController.viewControllers?.remove(at: 0) } – Alex Dec 17 '18 at 13:25