skip to Main Content

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]

2

Answers


  1. Chosen as BEST ANSWER

    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")
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search