2
votes

I've 5 UIViewcontroller in my UITabbarController but I need the show 4 of them in tabbar so I'm trying to remove last tabbar item from UITabbarController.

self.tabBarController?.tabBar.items?.removeLast()

But I'm getting this error.

Directly modifying a tab bar managed by a tab bar controller is not allowed

So how can I remove last tabbar item from tabbar ?

3
After I read your comment believe answer, I sill not understand what you want.Leang Socheat

3 Answers

4
votes

For removing a ViewController from tabBarController? you should simply implement:

tabBarController?.viewControllers?.removeLast()

Assuming that you remove the last controller (tab).

It is related to viewControllers:

An array of the root view controllers displayed by the tab bar interface.

Obviously, you could remove and controller, for instance, for removing the first controller:

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

Update:

First I need the open Fifth viewcontroller and I'll not show it in tabbar. User can't go fifth tabbar. Only I can navigate user. So cause of that I'm trying to do that.

For achieving this, all you should do is to let your storyboard to be structured as:

enter image description here

Considering that the first view controller (the one that should present the tabbar controller) is the view controller that you don't want to let it appears in the tabbar.

3
votes

You should manipulate the array of viewcontrollers in the tabbar controller. Try this.

if let tabBarController = self.tabBarController {
     let indexToRemove = 4
     if indexToRemove < tabBarController.viewControllers?.count {
         var viewControllers = tabBarController.viewControllers
         viewControllers?.remove(at: indexToRemove)
         tabBarController.viewControllers = viewControllers
     }
 }
0
votes

To remove a tab bar item of which you know the reference or location to, you can do it this way:

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