开发者

How to change color of selected bar button?

开发者 https://www.devze.com 2023-03-23 22:35 出处:网络
I have a simple black开发者_JAVA百科 navigation bar with following button When the button is pressed the button gets the same color as the the navigation bar with the same gradient. But I want it t

I have a simple black开发者_JAVA百科 navigation bar with following button

How to change color of selected bar button?

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:

How to change color of selected bar button?

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


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...

How to change color of selected bar button?


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..


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消