简体   繁体   中英

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?

  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: "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..

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM