Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set appearance for a UIButton without affecting UIBarButtonItems?

Using the following code to customize regular UIButtons also affects UIBarButtonItems and clear buttons in text fields.

[[UIButton appearance] setBackgroundImage:greenButtonImage forState:UIControlStateNormal];

I do not wish to customize the latter elements at all, only regular round rect buttons. I realize using appearanceWhenContainedIn: could be used to set a custom appearance for UIBarButtonItems and UITextField, but i wish for these buttons to remain standard. Subclassing is not an option here as it should not be required for such a simple task.

There is a similar question, but it does not address the issue. Why does -[[UIButton appearance] setBackgroundImage] affect the initial appearance of UIBarItem objects and how do you correct it?

like image 530
Maciej Swic Avatar asked Oct 10 '12 09:10

Maciej Swic


1 Answers

One solution I've used before is to nil out the "backgroundImage" property for UIButtons contained inside of a UINavigationBar:

[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:nil forState:UIControlStateNormal];

This should allow you to customize UIButtons in other cases, without touching the ones inside a UIBarButtonItem in the UINavigationBar.

like image 173
DiscDev Avatar answered Nov 14 '22 19:11

DiscDev