Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove TabBar item in Swift

I currently try to find a way to remove while run the app a TabBar Item, i found a way to enable or disable it but not to complete remove it.

For disable it i do:

enter image description here

In ViewDidLoad

if let tabBarItem = self.tabBarController?.tabBar.items?[3] as? UITabBarItem {
            tabBarItem.enabled = false
}

This works well but still the user can see the TabBar item and i ll simply complete remove it, is there a way?

I want to trigger the TabBarItem via Parse, if i set the Parse Data to true it should show other way it should not.

like image 378
Fabian Boulegue Avatar asked Feb 07 '15 16:02

Fabian Boulegue


People also ask

How do I hide and show tab bar in Swift?

If you don't want that behavior, you should set hidesBottomBarWhenPushed to true where applicable. This will hide the tab bar along with any toolbars you had showing, but only when a view controller is pushed onto the navigation stack. This allows you to show the tab bar at first, then hide it when you need more room.

How do I hide the bottom bar in Swift?

Answer: Use self. tabBarController?. tabBar. hidden instead of hidesBottomBarWhenPushed in each view controller to manage whether the view controller should show a tab bar or not.

How do I hide the tabBar in Objective C?

In case you're using tabbar with navigation controller hidesBottomBarWhenPushed will not work, but tabBarController. tabBar. hidden will do.


3 Answers

You want to set the viewControllers property of your tabBarController with an array where you excluded the particular viewController that you don't want to have anymore.

if let tabBarController = self.tabBarController {
    let indexToRemove = 3
    if indexToRemove < tabBarController.viewControllers?.count {
        var viewControllers = tabBarController.viewControllers
        viewControllers?.remove(at: indexToRemove)
        tabBarController.viewControllers = viewControllers
    }
}
like image 135
Daniele Avatar answered Oct 16 '22 18:10

Daniele


For those who just want to disable one item. Use this code from @Daniele's solution. and place it in your UITabBarController class

viewDidLoad() {

let index = 0 //0 to 5
viewControllers?.remove(at: index)

}
like image 13
Suhaib Avatar answered Oct 16 '22 19:10

Suhaib


Swift 5: For removing only one index in Tab Bar Controller(you can use this method in viewDidLoad and viewDidAppear both of them)

override func viewDidAppear(_ animated: Bool) {
    
}
override func viewDidLoad() {
    super.viewDidLoad()

}

tabBarController.viewControllers?.remove(at:0)  // for 0 index
tabBarController.viewControllers?.remove(at:1)  // for 1 index
tabBarController.viewControllers?.remove(at:2)  // for 2 index

if you have 4 index in Tab Bar and you want to remove the last 2 index

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

first line will remove the index 3rd one and you will remaining 3 from 4 and again when you remove the 2nd index it will remove again 3rd index and then you will have remain 2 index in last.

Another Way

//MARK: - Function Call
removeTab(at: 4)

//MARK: - Method
func removeTab(at index: Int) {
    if self.viewControllers?.count ?? 0 >= index {
        self.viewControllers?.remove(at: index)
    }
}
like image 5
Shakeel Ahmed Avatar answered Oct 16 '22 18:10

Shakeel Ahmed