How to set appearance for a UIButton without affecting UIBarButtonItems?
Asked Answered
V

1

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?

Vervain answered 10/10, 2012 at 9:39 Comment(2)
I was able to fix the UITextField problem by specifically setting those to a clear background image. Not a nice solution though.Vervain
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.Blanketing
B
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.

Blanketing answered 21/10, 2015 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.