Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable tabbar item - Swift

How do I disable a particular tabbar item? Something like for the 3rd icon...

self.tabBarItem.items![2].enabled = false

There must be a way of doing such a simple task as a one liner? The above doesn't work...

like image 885
Edward Hasted Avatar asked Jul 19 '15 08:07

Edward Hasted


2 Answers

Here is the answer

if  let arrayOfTabBarItems = tabBarViewController.tabBar.items as! AnyObject as? NSArray,tabBarItem = arrayOfTabBarItems[2] as? UITabBarItem {
        tabBarItem.enabled = false
    }
like image 129
Aditya Koukuntla Avatar answered Sep 24 '22 00:09

Aditya Koukuntla


Here's my code for doing the same, using Swift 3:

    let tabBarControllerItems = self.tabBarController?.tabBar.items

    if let tabArray = tabBarControllerItems {
        tabBarItem1 = tabArray[0]
        tabBarItem2 = tabArray[1]

        tabBarItem1.isEnabled = false
        tabBarItem2.isEnabled = false    
    }

Just put the block of code above in the viewDidLoad() method for starters and don't forget to create the tabBarItem variables and you're all good to go from there!

like image 36
Johan Tingbacke Avatar answered Sep 23 '22 00:09

Johan Tingbacke