Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change global UIBarButtonItem appearance

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?

like image 292
Jeroen Avatar asked Nov 11 '15 18:11

Jeroen


2 Answers

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.

like image 109
katleta3000 Avatar answered Oct 26 '22 02:10

katleta3000


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)
like image 43
JAL Avatar answered Oct 26 '22 02:10

JAL