9

I'm trying to set the font weight of a selected tab bar item to bold font. It seems as it has no effect. Any idea what is wrong. forState: .Normal works as expected, forState: .Selected has no effect.

let tabBarItem0 = tabBar.items![0] as! UITabBarItem
var selectedImage0 : UIImage = UIImage(named:"ic_tabbar_item_one")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var fontLight:UIFont = UIFont(name: "HelveticaNeue-UltraLight", size: 12)!
var fontBold:UIFont = UIFont(name: "HelveticaNeue-Bold", size: 12)!

tabBarItem0.image = unselectedImage0
tabBarItem0.selectedImage = selectedImage0
tabBarItem0.title = "Overview"
tabBarItem0.setTitleTextAttributes(
    [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: fontLight
    ], forState: .Normal)

tabBarItem0.setTitleTextAttributes(
    [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: fontBold
    ], forState: UIControlState.Selected)
Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177

7 Answers7

6

FOUND THE SOLUTION (Swift 3, XCode 8.1)

  1. In Storyboard, give a unique Tag to each UITabBarItem you have: For every tab -> Select it and go to it's "Attributes Inspector" -> Give each one a unique number in the "Tag" field but you should not use zero (I used 1 through 4).

    This sets us up for later, to identify which tab is pressed.


  1. Create a new subclass of UITabBarController and then assign it: FILE -> New File -> iOS Cocoa Touch -> create a Subclass of UITabBarController. Assign the new .swift file to your UITabBarController under "Identity Inspector."

    We will need custom logic in our UITabBarController.


  1. Create a new subclass of UITabBarItem, assign the same file to all of your UITabBarItems: FILE -> New File -> iOS Cocoa Touch -> create a Subclass of UITabBarItem and assign the same one to all of your tabs.

    We will need a shared custom logic in our tab bar items.


  1. Add this code to your UITabBarItem subclass, it sets up the initial state (primary tab bold, the rest unselected) and will allow for programmatic tab changes as well:

    class MyUITabBarItemSubclass: UITabBarItem {
    
        //choose initial state fonts and weights here
        let normalTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightRegular) 
        let selectedTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightBold)
    
        //choose initial state colors here
        let normalTitleColor = UIColor.gray 
        let selectedTitleColor = UIColor.black
    
        //assigns the proper initial state logic when each tab instantiates 
        override func awakeFromNib() {
            super.awakeFromNib()
    
            //this tag # should be your primary tab's Tag* 
            if self.tag == 1 { 
                self.setTitleTextAttributes([NSFontAttributeName: selectedTitleFont, NSForegroundColorAttributeName: selectedTitleColor], for: UIControlState.normal)
            } else {
                self.setTitleTextAttributes([NSFontAttributeName: normalTitleFont, NSForegroundColorAttributeName: normalTitleColor], for: UIControlState.normal)
            }
    
        }
    
    }
    

Here we set up the initial state so that the tabs are set correctly when the app opens up, we'll take care of the physical tab presses in the next subclass.


  1. Add this code to your UITabBarController subclass, it's the logic for assigning the correct states as you press on the tabs.

    class MyUITabBarControllerSubclass: UITabBarController {
    
        //choose normal and selected fonts here
        let normalTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightRegular)
        let selectedTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightBold)
    
        //choose normal and selected colors here
        let normalTitleColor = UIColor.gray
        let selectedTitleColor = UIColor.black
    
    
        //the following is a delegate method from the UITabBar protocol that's available 
        //to UITabBarController automatically. It sends us information every
        //time a tab is pressed. Since we Tagged our tabs earlier, we'll know which one was pressed,
        //and pass that identifier into a function to set our button states for us
    
        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
            setButtonStates(itemTag: item.tag)
        }
    
    
        //the function takes the tabBar.tag as an Int
        func setButtonStates (itemTag: Int) {
    
            //making an array of all the tabs
            let tabs = self.tabBar.items
    
            //looping through and setting the states
            var x = 0
            while x < (tabs?.count)! {
    
                if tabs?[x].tag == itemTag {
                    tabs?[x].setTitleTextAttributes([NSFontAttributeName: selectedTitleFont, NSForegroundColorAttributeName: selectedTitleColor], for: UIControlState.normal)
                } else {
                    tabs?[x].setTitleTextAttributes([NSFontAttributeName: normalTitleFont, NSForegroundColorAttributeName: normalTitleColor], for: UIControlState.normal)
                }
    
                x += 1
    
            }
    
        }
    
    }
    

It looks like this was such a pain because for some reason the tabs do not recognize state changes into ".Selected". We had to do everything by working with .Normal states only - basically detecting the state changes ourselves.


  1. You can programmatically change the tabs and still detect state changes by... I'll update this later if anyone has an interest, just ask.

Hope this helped!

Abhi
  • 270
  • 3
  • 12
2
UITabBarItem.appearance().setTitleTextAttributes(
        [NSFontAttributeName: UIFont(name:"your_font_name", size:11)!, 
            NSForegroundColorAttributeName: UIColor(rgb: 0x929292)], 
        forState: .Normal)
2

I've faced the same issue when tried to change font of selected item. Looks like titleTextAttributes' font parameter is only useful when setting them to normal state. That's why I implemented UITabBarControllerDelegate where I update attributes for currently selected item. You should call updateSelection() method after UITabBarControllers loadView() too. Or you can call updateSelection() method in overridden selectedItem setter.

extension TabBarController: UITabBarControllerDelegate {

  func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    updateSelection()
  }

  func updateSelection() {
    let normalFont = Fonts.Lato.light.withSize(10)
    let selectedFont = Fonts.Lato.bold.withSize(10)
    viewControllers?.forEach {
      let selected = $0 == self.selectedViewController
      $0.tabBarItem.setTitleTextAttributes([.font: selected ? selectedFont : normalFont], for: .normal)
    }
  }

}
Timur Bernikovich
  • 5,660
  • 4
  • 45
  • 58
2

Build Settings\Swift Language Version: 4.1

General\Deployment Target: 10.3

import UIKit

class FirstViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()

    let attrsNormal = [NSAttributedStringKey.foregroundColor : UIColor.black,
                 NSAttributedStringKey.font : UIFont(name: "Arial", size: 14)!]
    UITabBarItem.appearance().setTitleTextAttributes(attrsNormal,
                                                     for: UIControlState.normal)

    let attrsSelected = [NSAttributedStringKey.foregroundColor : UIColor.red,
                       NSAttributedStringKey.font : UIFont(name: "Arial", size: 14)!]
    UITabBarItem.appearance().setTitleTextAttributes(attrsSelected,
                                                     for: UIControlState.selected)
  }
  ...
}
Alexander Lubyagin
  • 1,346
  • 1
  • 16
  • 30
2

When I try to change font of selected tab by using UITabBarItem but it doesn't work. Adoption of delegate is suggested to get informed when tab changes. I just put pieces together.


private class MyTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }

    override func setViewControllers(_ viewControllers: [UIViewController]?, animated: Bool) {
        super.setViewControllers(viewControllers, animated: animated)
        updateTabBarAppearance()
    }
}

extension MyTabBarController: UITabBarControllerDelegate {

    public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        updateTabBarAppearance()
    }

    func updateTabBarAppearance() {
        self.tabBar.tintColor = .red
        let unselectedLabelFont = UIFont.systemFont(ofSize: 18, weight: .regular)
        let selectedLabelFont = UIFont.systemFont(ofSize: 20, weight: .semibold)
        let labelColor = UIColor.init(red: 38/255.0, green: 38/255.0, blue: 38/255.0, alpha: 1.0)
        let selectedIconColor = UIColor.init(red: 8/255.0, green: 8/255.0, blue: 8/255.0, alpha: 1.0)

        viewControllers?.forEach {
            let isSelected = $0 == self.selectedViewController
            let selectedFont = isSelected ? selectedLabelFont : unselectedLabelFont
            $0.tabBarItem.setTitleTextAttributes([.font: selectedFont, .foregroundColor: labelColor], for: .normal)

        }
    }
} 
1

The problem is that the state of tabBarItem0 is not changed to Selected. Because this is UITabBarItem which represents a single element of a UITabBar. So, you can not directly change the status using UITabBarItem API. You have to change it state by assigning selectedItem.

This information is gained from documentation and I suggest all programmers to have skills like this. Hopefully, this will help.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
  • `setItems` is setting the items to the tabbar and is used when you want to add, remove or reorder items in the tabbar. This has nothing to do, at least for my understanding, with the select state: https://goo.gl/LfEIKH – Thomas Kremmel Jul 14 '15 at 06:50
  • I just update the answer. You should assign to the `selectedItem` property of `UITabBar`. @ThomasKremmel – Lucas Huang Jul 14 '15 at 18:57
1

To set the TitleTextAttribute, you should use the appearance proxy like: [UIBarItem appearance]

rmp
  • 3,503
  • 1
  • 17
  • 26
  • He is asking why the appearance did not change. Not asking how to do it. – Lucas Huang Jul 13 '15 at 17:27
  • Yes, I know that is what he is asking. I believe the reason is that it is not being set correctly. – rmp Jul 13 '15 at 17:28
  • But the `appearance` is set for the whole app, he probably just wants to set it in this particular case I assume? I think last two lines of codes are correct, it's just not changing to the correct state. Any ideas? @rmp – Lucas Huang Jul 13 '15 at 17:31
  • Per the docs: "In iOS v5.0 and later, you can customize the appearance of tab bars by setting item label text attributes using appearance selectors declared by UIBarItem. " – rmp Jul 13 '15 at 17:34
  • Yes, that's correct. But that will apply for every single `UIBarItem`. And I don't say you are wrong. It's just the last two lines of codes are correct as well. The problem is he does not know how to change it to `Selected` status. @rmp – Lucas Huang Jul 13 '15 at 17:36
  • The last two lines of code are not correct, if they were the OP's code would work and there would not have been a post. Also nowhere in the OP does is state that it is for a single Tab, just that when a tab is selected. It would seem odd to not apply the bold selected state to all tabs. Let's just agree to disagree as this is not constructive. – rmp Jul 13 '15 at 18:00
  • I think both of your answers are not correct ;-) The docs say "you can customize the title’s text attributes using setTitleTextAttributes:forState:, either for a single item, or for all items by using the appearance proxy " . Thus setting it for a single item as I do is fine. https://goo.gl/oX5KW4 – Thomas Kremmel Jul 14 '15 at 06:55
  • I even tried it using the `appearance` proxy from `UIBarItem`. Same effect -> no result. I start to consider this to be a Simulator bug. Will try it on real hardware. `UIBarItem.appearance().setTitleTextAttributes( [ NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: fontBold], forState: UIControlState.Selected)` – Thomas Kremmel Jul 14 '15 at 07:05