DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

How to change color of selected bar button?

I have a simple black navigation bar with following button

enter image description here

When the button is pressed the button gets the same color as the the navigation bar with the same gradient. But I want it to look like this:

enter image description here

So this has a simple black background color. Any idea how to achieve this with interface builder? Or does this require a programmatic way?

Upvotes: 3

Views: 2766

Answers (3)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73608

This is easy, first you need to do this. Goto your interface builder. Follow these steps -

  1. Select the UIButton element. Open the right-hand sidebar which gives attributes about the button. Select Type of your button as Custom.
  2. Now in there, look for state config. The various states for a button - default, highlighted, selected, disabled. Define various different images as you want in image. Basically select images such that the effect you want is satisfied.
  3. Now, this automatically handles change in appearance of the button element depending on the state of the button.
  4. But sometimes, you would want to change the state manually, through code. This can be done like so - [yourButton setSelected:BOOL] or [yourButton setHighlighted:BOOL] or [yourButton setEnabled:BOOL]

Hope this helps...

Update: See the screenshot - This shot shows that there are states defined, 'default', 'highlighted'... this defines the states. You can define images for each of these states. Again, hope this helps...

enter image description here

Upvotes: 2

Anomie
Anomie

Reputation: 94804

The only supported way to control the appearance beyond specifying the text or greyscale image and the UIBarButtonItemStyle of a UIBarButtonItem in iOS 4 is to design it as a view (probably a subclass of UIButton) and wrap that view in a UIBarButtonItem using initWithCustomView:.

Or you could skip using UIToolbar completely, and just place UIButtons over a UIImageView to achieve the visual effect you desire.

Upvotes: 1

Saurabh
Saurabh

Reputation: 22873

Yes it require some programming.. but if you wants to do this using IB then you can use one dirty way.. You can use Images for all these controls.. (Yes thats why this is dirty) if you have psd of you design then you can slice each element. Like you can slice the tool bar background (black bar) and can place it as an image at specific location. then you can add a button on it and set its image (for normal and highlighted state)

I'll try to do it by coding and if I found a way then I'll post that here..

Upvotes: 2

Related Questions