20

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.

Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72

5 Answers5

33

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
    }
}
Daniele
  • 2,461
  • 21
  • 16
  • 1
    do you know how to get it to come back after you've removed it? – Adam Mar 31 '17 at 01:49
  • I know this is quite old now, but regarding 'how do you get it back?' - keep a reference to the original (e.g. in viewDidLoad take a copy of the original viewControllers array) so that you can revert back to it whenever you wish. – Ali Beadle Jun 04 '17 at 13:16
  • @Adam Did you find any solution to add it back? – Ashutosh Shukla Dec 04 '19 at 10:45
  • Note: Do not remove ViewControllers inside TabBarController. Remove them when you are pushing TabBarController screen. – Chintan Shah Apr 17 '20 at 08:22
  • As a note I ran this code to remove a tab bar item in the view did load of my first screen, but it crashed due to an out of bounds error with the view controller's array. I forcefully ran this on the main thread using DispatchQueue and it worked afterwards – Andrew Feb 15 '21 at 21:09
13

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)

}
Suhaib
  • 2,031
  • 3
  • 24
  • 36
5

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)
    }
}
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
3

Swift 4.1 For removing More items Use Array

let index = [2,0]
index.forEach{viewControllers?.remove(at: $0)}

the point in the array is You Should Use Descending Order of indexes to remove to get the desired Result.

Ali Pishvaee
  • 303
  • 5
  • 7
-10

better way is to use only text instead of image. choose 'space' as the text then disable that. then the user will not be able to see it. i haven't tested it but I sure it will work.