Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking a UIBarButtonItem selected state?

Is there a way to highlight (i.e., toggle) a UIBarButtonItem without using a custom view?

For example, see 3D button from the Maps app:

Maps app

or Shuffle All from the Music app:

Music App

like image 561
romaonthego Avatar asked Jan 06 '14 23:01

romaonthego


1 Answers

You can set the background image of the UIBarButtonItem:

item.setBackgroundImage(UIImage(named: "item-bg.png"), forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

Then when you want to de-select it, set the background image to nil:

item.setBackgroundImage(nil, forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)

(You have to create an image file for the background)

like image 196
Jeffrey Sun Avatar answered Oct 05 '22 01:10

Jeffrey Sun