Search code examples
xcodeswiftfontsnavigationuinavigationbar

Change BarButtonItem Font Using Swift (Xcode 6)


I am having trouble changing the font in my swift Xcode project of a BarButtonItem that was added to a navigation controller. I was able to change the color of the button without a problem, but the font will not change. Code:

var navTextColor = UIColor(red:0.3, green:0.09, blue:0.05, alpha:1.0)
self.navigationController?.navigationBar.tintColor = navTextColor

Solution

  • If you create and outlet (e.g. @IBOutlet var barButton: UIBarButtonItem!) linked to your UIBarButtonItem, you should be able to change your font type by using setTitleTextAttributes on the outlet.

    barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], forState: UIControlState.Normal)
    

    Swift3

    barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], for: UIControlState.normal)