Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing UIButton background color

Tags:

objective-c

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.

like image 737
Karthikeya Avatar asked Mar 16 '12 12:03

Karthikeya


People also ask

How to change UIButton background color in Swift?

Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.

How do you highlight a button in Swift?

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.


2 Answers

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]];
}
like image 62
dasdom Avatar answered Nov 22 '22 14:11

dasdom


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

like image 31
silvaric Avatar answered Nov 22 '22 12:11

silvaric