I am creating buttons programmatically. I want to change button background color while touching up inside again it has to set back to its usual color after lifting up our finger.....
nine = [UIButton buttonWithType:UIButtonTypeCustom];
[nine setFrame:CGRectMake(15, 105, 65, 40)];
[nine setTitle:@"9" forState:UIControlStateNormal];
[nine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[nine setBackgroundColor:[UIColor cyanColor]];
[nine addTarget:self action:@selector(clickDigit:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nine];
// to change background color
-(void)changeButtonBackGroundColor:(id) sender
{
[nine setBackgroundColor:[UIColor redColor]];
}
Here changeBackgroundColor method was created to change color of that button . it changes color.
Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.
In storyboard choose the UIButton you want to change. In the identity inspector in the right corner there is a filed named "class" there type "HighlightedButton" and press enter. Now your button will change color to red when it is highlighted and back to green when you release the button.
Don't know if this relates to your question but: this
[nine setBackgroundColor:[UIColor redColor]];
should be
[sender setBackgroundColor:[UIColor redColor]];
Edit: Change this
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
to
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchDown];
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
[nine addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchCancel];
and add the method:
- (void)resetButtonBackGroundColor: (UIButton*)sender {
[sender setBackgroundColor:[UIColor cyanColor]];
}
I think you have two options..
First:
You can put the [nine setBackgroundColor:[UIColor redColor]]; inside "clickDigit" (and like dasdom said rename to sender and change to (UIButton*)sender)..
Change
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
to
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpOutside];
and the method "changeButtonBackGroundColor"
[sender setBackgroundColor:[UIColor cyanColor]];
Second
create universal UIControlEvents
[nine addTarget:self action:@selector(changeButtonBackGroundColor:) forControlEvents:UIControlEventAllEvents];
-(void)changeButtonBackGroundColor:(UIButton*) sender{
if ([sender.backgroundColor isEqual:[UIColor redColor]]){
[sender setBackgroundColor:[UIColor cyanColor]];
}else{
[sender setBackgroundColor:[UIColor redColor]];
}}
I didn't try this code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With