1

I'd like to change the UITabBarControllers text color after it's been loaded based on the users actions.

I can call this, and it changes the font and color correctly when it launches.

 let appearance = UITabBarItem.appearance()
 let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.green]
 appearance.setTitleTextAttributes(attributes, for: .normal)

When I try to call this anytime after the app has fully launched though, nothing happens.

Is this possible, or is it a one time thing you can do at launch?

Ben987654
  • 2,534
  • 2
  • 21
  • 41
  • Can you check with `UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Selected)` or for Normal state do `.Normal` – Mukesh Feb 22 '17 at 05:23
  • Or check with `tabBarController.tabBar` tintColor – Mukesh Feb 22 '17 at 05:28
  • You should try this - http://stackoverflow.com/questions/31117069/changing-tab-bar-item-image-and-text-color-ios – Prema Janoti Feb 22 '17 at 05:30
  • Setting the tint color works that way, but I don't want to change the tint color, I want to change the normal font color. I tried UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .normal) which does nothing for .normal and .selected when it's called after it's loaded. It does work pre-loaded. I'll take a closer look at that stack link, but all of that seems to be about setting it up before it loads or dealing with the images? – Ben987654 Feb 22 '17 at 05:47

1 Answers1

0

It looks like if i iterate over all the items and set them manually it works. I then need to re-set the selected items color.

Still not sure why the other way won't work?

for myItem in (tabBar.items)! {
    myItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .normal)
}

tabBar.selectedItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
Ben987654
  • 2,534
  • 2
  • 21
  • 41