Remove TabBar item in Swift

风格不统一 提交于 2019-11-28 11:10:14

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

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)

}

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!