9

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?

Community
  • 1
  • 1
Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
  • 1
    I was able to fix the UITextField problem by specifically setting those to a clear background image. Not a nice solution though. – Maciej Swic Oct 10 '12 at 10:53
  • How are you initializing your UIBarButtonItems? Are you setting the "customView" property to be a UIButton object? If you can provide sample code for how you create a UIBarButtonItem then I'm pretty sure I can help...I do lots of customisations with appearance proxies and bar buttons/UIButtons in my apps and haven't had these issues. – DiscDev Oct 21 '15 at 13:57

1 Answers1

4

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.

DiscDev
  • 38,652
  • 20
  • 117
  • 133