Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the tintColor of a UIBarButtonItem via the appearance protocol?

I'm trying to programatically set the tintColor for UIBarButtonItems throughout my project. I'm calling:

[UIBarButtonItem appearance]

However, after looking at Apple's documentation here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html

I found no such properties for setting the tintColor. The closest related methods I could find were:

setBackgroundImage:forState:barMetrics:
setBackgroundImage:forState:style:barMetrics:

However, both of these methods are for setting an actual background image. Is it not possible to simply set the button's tintColor via UIAppearance?

like image 980
Jim Jeffers Avatar asked Dec 09 '22 17:12

Jim Jeffers


2 Answers

Maybe I dont understand the question correctly. But if you look in the link that you've posted, under Tasks -> Customizing Appearance, there's a property called tintColor.

You just have to set that property to the desired color

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

Is this not working for you?

like image 115
Dhruv Goel Avatar answered Feb 01 '23 23:02

Dhruv Goel


As per the UIAppearance Protocol, you can set the tintColor as below :

To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, you use appearanceWhenContainedIn: to get the appearance proxy for the class

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
       setTintColor:myNavBarColor];

This is mentioned here , if that's what you are looking for.

like image 32
Bikramjit Singh Avatar answered Feb 01 '23 23:02

Bikramjit Singh