2

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 ?

Murat Kaya
  • 1,281
  • 3
  • 28
  • 52

3 Answers3

4

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.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • I just need the remove last item not viewcontroller from tabbar – Murat Kaya Jul 18 '17 at 07:11
  • Please elaborate, if the last item from the tabbar has been removed, then how you can navigate to its viewcontroller? by default removing a view controller from the `viewControllers` array should automatically remove its item in the tabbar. – Ahmad F Jul 18 '17 at 07:12
  • I'll not navigate this viewcontroller again. So I just need the remove last item from tabbar not viewcontroller – Murat Kaya Jul 18 '17 at 07:20
  • @MuratKaya Exactly! that's why you should remove the viewcontroller from 'tabBarController?.viewControllers?' and the tabbar will displayed as it should, by displaying items only for the needed view controllers. Have you tried to implement it anyway :)? – Ahmad F Jul 18 '17 at 07:22
  • 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. – Murat Kaya Jul 18 '17 at 07:41
3

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
     }
 }
Rish K
  • 664
  • 7
  • 14
  • 1
    Instead of copy, paste [this answer](https://stackoverflow.com/a/28384731/5501940), you should comment it... – Ahmad F Jul 18 '17 at 07:11
0

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)
Ali
  • 481
  • 3
  • 16
  • Note that it is kind of danger to implement this without any check, if the `tabBarController?.viewControllers?` contains less than 5 view controllers the app will crash. – Ahmad F Jul 18 '17 at 07:16
  • As I said, if you know the reference to that particular controller you're removing, you may use this. – Ali Jul 18 '17 at 07:19