简体   繁体   中英

How to programmatically change Tab Bar Item image and font when selected?

I would like to change the weight of my Tab Bar Item and font when it is selected. I assume this can be done by changing the Tab Bar Item Image and text when the associated tab is being selected. Apart from adjusting the tintcolor of highlighted tab bar items, I can't seem to find any information customising selected Tab Bar Items online even though most apps (including Instagram) do it. How is this done?

My current 5 tab bars are created in a UITabBarController Class and follow an almost identical formula like this:

let homeController = HomeViewController()
        homeController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home"), tag: 1)
        let nav1 = UINavigationController(rootViewController: homeController)

viewControllers = [nav1, nav2, nav3, nav4, nav5]

Programmatically set selected UITabbarItem 's image:

let tabBarItem = UITabBarItem(title: "title", image: UIImage(named: "defaultImage"), selectedImage: UIImage(named: "selectedImage"))

You can't as easily set selected UITabbarItem 's font though. You'd need to create your own UITabBarController as shown in this SO thread .

In order to change the image I simply enter this code in the associated view controller class (in my case they are individuals swift files)

so in viewDidLoad of class HomeViewController: UIViewController {

self.tabBarItem.selectedImage = UIImage(named: "yourImageName")

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