Search code examples
iosswift3uitabbaritemuitabcontroller

How to disable tabs in UITabBarController?


Before I updated to Swift 3, this worked perfectly (besides isEnabled was just enabled). This code is in my UITabBarController ViewDidLoad function. The if is never met so therefore the items are never set to true.

How do I access the items as I want the tabs to be greyed about until a variable is selected?

if  let arrayOfTabBarItems = self.tabBar.items as AnyObject as? NSArray,let tabBarItem = arrayOfTabBarItems[1] as? UITabBarItem {
    tabBarItem.isEnabled = true
}

Solution

  • Try this:

    let arrayOfTabBarItems = self.tabBar.items
    
    if let barItems = arrayOfTabBarItems, barItems.count > 0 {
      let tabBarItem = barItems[0]
      tabBarItem.isEnabled = true
    }