UIButton Highlight issue - iphone

I have an UIButton in my application.
I want to show a selection of my button. I am using this code:
[Button setHighlighted:YES];
But it's not working.
Can anyone help me?

You can solve this problem by having two images for button. One for highlighted state and one for normal state. When you create the button, create with normal state image as buttons background image. And when you select or focus on the button, change the image as highlighted background image.
[Button setBackgroundImage:[UIImage backgroundImage:#"ButtonSelected.png"] forState:UIControlStateSelected];
[Button setBackgroundImage:[UIImage backgroundImage:#"ButtonNormal.png"] forState:UIControlStateNoraml]

See these posts.
http://forums.macrumors.com/showthread.php?t=530998
http://simonwoodside.com/weblog/2009/2/5/how_to_make_an_iphone/

Related

How to make image behind invisible button look like it was pressed down?

Hi I have a couple of images that are covered by an invisible button. Is there a way that I can make it look like the images were pressed down when I press the invisible button? Thanks!
You can put the image directly onto the button, you dont need a seperate UIImageView.
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateSelected];
The are a few more UIControlState constants.
Take a look at the docs:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState

iphone button text color changes by itself when clicked?

Im trying to troubleshoot my first iphone app and Ive run into a propblem i just dont get. I have lots of buttons on a view and when you click them their text colour changes from black to blue. I assume that I could fix this by just setting the textColor property every time the button gets pushed to set it back to black but I feel like there must be something else going on.
Anyone know why my buttons are changing color by themselves?
It is possible to have different title colors depending on button state. Set them using the following method of the UIButton class:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
F.e.
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
where UIControlStateNormal is the default, not pressed, button state, and forState:UIControlStateHighlighted represents the pressed state. But there are more, and can be combined with binary sum, such as (UIControlStateHighlighted | UIControlStateSelected).

how to set image in button

How set image in button when ever click the button image show me that button
run.backgroundColor = [UIColor redColor] I try this but it doesn't work,
To set an image for a button while it is currently being touched (I think this is what you're asking). You use setImage: forState on the UIButton.
[myButton setImage:[UIImage imageNamed:#"myButtonGraphic.png"] forState:UIControlStateHighlighted];
You can control when the image appears using the different control states, one of:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
UIControlStateApplication
UIControlStateReserved
If you use UIControlStateNormal, the image is displayed all the time, and when the button is currently being touched it will appear dimmer which for a lot of cases is good enough to indicate to the user that they are selecting that button and that it's a real touchable 'thing'.

iPhone: UIButton don't use default pressed highlight

Currently when you use a UIButton and press down on it, it gives the button a dark overlay as an indicator that the button was pressed. I don't want this as I am implementing my own pressed state.
How can I remove this default?
You could also set the appropriate property:
button.adjustsImageWhenHighlighted = NO;
You should be able to set it by setting the image for the control state. Think its
[button setBackgroundImage:[UIImage imageNamed:#"Selected_day.png"] forState:UIControlStateHighlighted];

How to highlight the button untill the next view is changed in iphone?

I have created five buttons in the view controller. If i clicked the button it goes to the corresponding view. Now i want to display the button in highlighted state when it is clicked. It should go back to the normal state only when i click the other button. I have set the another image for highigthting buttons when i clicked it, but it shows that highlighted state only one sec. Now i want to display the buttons highlighted till another button is clicked. Same like a Tabbar operations.(I have used buttons instead of tabbar for the requirements).
Now i have used the following code,
void didLoad
{
[btn1 setImage:[UIImage imageNamed:#"ContentColor.png"] forState:UIControlStateHighlighted];
[btn2 setImage:[UIImage imageNamed:#"bColor.png"] forState:UIControlStateHighlighted];
[btn3 setImage:[UIImage imageNamed:#"ShColor.png"] forState:UIControlStateHighlighted];
[btn4 setImage:[UIImage imageNamed:#"PicturesColor.png"] forState:UIControlStateHighlighted];
[btn5 setImage:[UIImage imageNamed:#"infoColor.png"] forState:UIControlStateHighlighted];
}
Please help me out.
Thanks.
You should be manipulating the image for selected state, not for highlighted state. Highlight will be unset on touch up or touch outside, as you have seen, whereas selected is persistent.
Simple solution would be...
on every click on any button change it's image (For normal state) to highlighted image for that particular button and set other 4 button's image to normal image...
It sounds like you should be using a UISegmentedControl with 5 segments instead of 5 buttons. It behaves this way already, a segment stays highlighted until you select a different one (unless you set the momentary property).