Hamza Imran
Hamza Imran

Reputation: 189

Change Button Text Color When Clicked in IOS

I know this question is asked frequently and many of answers are correct, I have my code in which I'm facing an issue. I have radio buttons on which when user click the button background color and text color changes. The background color is changed as I expected but the text color turns blue area when the button is clicked. I have coded it to change white color when click but when I click the button it changes the text area to blue, I'm confused that where I'm doing mistakes. My code is,

 self.selectedTitleColor = [UIColor whiteColor];
self.selectedBackgroundColor = [UIColor redColor];

self.unselectedTitleColor = [UIColor blackColor];
self.unselectedBackgroundColor = [UIColor whiteColor];

- (IBAction)House:(id)sender {

[sender setSelected: ![sender isSelected]];
[self.flat setSelected:NO];
[self.lowerP setSelected:NO];
[self.upperP setSelected:NO];
[self.farmH setSelected:NO];
[self.pentH setSelected:NO];
[self.roomB setSelected:NO];


[self updateButtonColors];

}

-(void)updateButtonColors {
if (self.house.selected) {
    [self.house setTitleColor:self.selectedTitleColor forState:UIControlStateNormal];
    [self.house setBackgroundColor:self.selectedBackgroundColor];

    House=_house.titleLabel.text;
    ImageViewD = [NSString stringWithFormat:@"%@",House];

    NSLog(@"HHHH  %@",ImageViewD);

} else {
    [self.house setTitleColor:self.unselectedTitleColor forState:UIControlStateNormal];
    [self.house setBackgroundColor:self.unselectedBackgroundColor];
        }

}

enter image description here

Upvotes: 3

Views: 1619

Answers (2)

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

Best to give all property into Storyboard itself. You can take two images for your color and set into UIButton's backgroundImage state.

Check out below image for same.

[Normal State]

Selected State

I have a binding button with below method and its code:

- (IBAction)clicked:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
}

Below are images which I took it for reference.

Red.png

Green.png

Hope it will help you.

Upvotes: 0

KKRocks
KKRocks

Reputation: 8322

Change button type in storyboard from system to custom .

enter image description here

Upvotes: 2

Related Questions