grabury
grabury

Reputation: 5559

How to disable a tab bar item?

Is it possible to hide or disable a tab bar item on a tab bar throughout the entire app for a certain use case?

Example: While the user is logged in, and they do not have a Role of 'manager', the last tab bar item will be hidden throughout the app. When they log in again as a manager the last tab bar will be enabled and not hidden.

Upvotes: 2

Views: 8399

Answers (4)

quin
quin

Reputation: 474

Swift 5 one liner

let n = 2.   //The tab number to disable
self.tabBarController!.tabBar.items![n].isEnabled = false

Upvotes: 1

SwiftBuild94
SwiftBuild94

Reputation: 53

Better solution (in Swift 5)

tabBarControlled?.tabBar.items?[2].isEnabled = isManager

Upvotes: 3

Sunkas
Sunkas

Reputation: 9590

Better code (in Swift 4):

tabBar.items?.forEach { $0.isEnabled = false }

Upvotes: 3

Himanshu
Himanshu

Reputation: 360

If you are inside the source file of the UITabBarController, just add below code in viewDidLoad method to disable last item

Also below code assumes you are having UITabBarItem items in tab bar. Otherwise you know what type of item is it so you can cast accordingly

if let items = tabBar.items as? [UITabBarItem] {
  if items.count > 0 {
    let itemToDisable = items[items.count - 1]
    itemToDisable.enabled = false
  }
}

Upvotes: 3

Related Questions