Change the appearance of a UIBarButtonItem

I am using Swift to create an iOS application and want to change the global appearance of UIBarButtonItem . I do this in didFinishLaunchingWithOptions .

Apple documentation says the following:

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

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

+5
source share
2 answers

Have you tried to figure this out with the UIAppearance protocol? It should be used for global modeling of some visual classes. I can suggest your code as follows:

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

It is hoped that the code shows how this works and how you can exchange it.

+5
source

Call setTitleTextAttributes on the UIBarButtonItem external proxy :

Swift 3:

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

Swift 2.x:

 UIBarButtonItem.appearance().setTitleTextAttributes([key : value], forState: UIControlState.Normal) 
+6
source

Source: https://habr.com/ru/post/1235725/


All Articles