8

I am using Swift to build an iOS application and would like to change the global appearance of UIBarButtonItem. I am doing this in didFinishLaunchingWithOptions.

Apple's documentation says this:

func setTitleTextAttributes(_ attributes: [String : AnyObject]?, forState state: UIControlState)

for UIBarItem. But when I try to do this, it only expects self: UIBarItem. Has anyone else encountered this? Is it a bug in Xcode or am I doing something wrong?

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Jeroen
  • 2,011
  • 2
  • 26
  • 50
  • This has been answered many times before. Check this out : http://stackoverflow.com/a/28347428/469614 – Vexy Dec 28 '16 at 12:28

2 Answers2

8

Call setTitleTextAttributes on the UIBarButtonItem appearance proxy:

Swift 3:

UIBarButtonItem.appearance().setTitleTextAttributes([key : value], for: .normal)

Swift 2.x:

UIBarButtonItem.appearance().setTitleTextAttributes([key : value], forState: UIControlState.Normal)
JAL
  • 41,701
  • 23
  • 172
  • 300
8

Did you tried realising it with UIAppearance protocol? It should be used for global сustomizing some visual classes. I can suggest your some code like this:

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UINavigationBar.classForCoder()]).setTitleTextAttributes(["attribute" : "value"], forState: .Normal)

Hopes it is clear from the code the way in works and how you can enwide it.

katleta3000
  • 2,484
  • 1
  • 18
  • 23