bertabus
bertabus

Reputation: 92

change storyboard tab bar icon using swift

I've created a tab bar controller and embedded several view controllers in it. I then try to set a specific tab bar item in a corresponding swift file for that view. Problem is that I am changing the item by overriding the "viewDidLoad" function. So it only updates after the user has touched that item. What be a better approach to changing storyboard tab bar items using swift?

Upvotes: 0

Views: 951

Answers (1)

HasanRaza
HasanRaza

Reputation: 104

  1. create a subclass of UITabBarController
  2. set this custom class of your TabBarController
  3. now you override UITabBarController ViewDidLoad Method
  4. there you can access all the TabItems and change their text/images before a ViewController get loaded.

       class CustomTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let allItems:[AnyObject] = self.tabBar.items!
        var item1:UITabBarItem = allItems[0] as! UITabBarItem
        var item2:UITabBarItem = allItems[1] as! UITabBarItem
    
    
        item1.image = UIImage(named: "[email protected]")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        item2.image = UIImage(named: "[email protected]")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    }
    

    }

in custom class the code should be like as above... you can set selectedState Image as well....

here is the result..

enter image description here

Upvotes: 1

Related Questions