Search code examples
iphoneioscustomizationuibarbuttonitemuiappearance

Button image for UIControlStateSelected not working after customizing UIBarButtonItem through UIAppearance


I've customized UIBarButtonItem in my AppDelegate.m using UIAppearance and all the bar buttons are showing the correct images in normal and selected states.

I want "Done" and "Save" buttons to use a different image (so they appear to be blue instead of gray), so I use -setBackgroundImage: forState: barMetrics to set two different images for UIControlStateNormal and UIControlStateSelected in -viewDidLoad.

The image for UIControlStateNormal is working fine, but the UIControlStateSelected one isn't showing, instead it shows the original image I used for selected states in AppDelegate.m. If I comment out the original image the new (blue) one is shown properly.

What's the proper way to set different background images for specific buttons?


Solution

  • I believe UIControlStateHighlighted is what you are looking for.

    A UIBarButtonItem goes into the highlighted state whenever it receives the user touch. UIControlStateSelected is typically for UI elements which toggle between two states.