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?
Asked
Active
Viewed 942 times
1 Answers
1
- create a subclass of UITabBarController
- set this custom class of your TabBarController
- now you override UITabBarController ViewDidLoad Method
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: "menu@2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) item2.image = UIImage(named: "play@2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) }
}
in custom class the code should be like as above... you can set selectedState Image as well....
here is the result..

HasanRaza
- 106
- 1
- 4