简体   繁体   中英

How to remove Tabbar Item in swift 3

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 ?

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
     }
 }

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:

在此处输入图片说明

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.

要删除您知道其引用或位置的选项卡栏项目,可以通过以下方式进行操作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM