Kaitis
Kaitis

Reputation: 638

Simulating UISegmentedControl with 2 UIButtons

I want to have two custom buttons to simulate how a UISegmentedControl behaves (switch button text color and image)

Here is what I have so far:

    - (IBAction)byDateTapped:(UIButton *)sender {

    UIColor *darkColor = [UIColor colorWithRed:52/255 green:91/255 blue:120/255 alpha:1.0];
    UIColor *lightColor = [UIColor colorWithRed:158/255 green:200/255 blue:217/255 alpha:1.0];

    self.byDateView.hidden = NO;
    [self.byDateButton setBackgroundImage:[UIImage imageNamed:@"radioOn"] forState:UIControlStateNormal];
    [self.byDateButton setTitleColor:darkColor forState:UIControlStateNormal];
    [self.byDateButton setTitleColor:darkColor forState:UIControlStateHighlighted];

    self.byCategoryView.hidden = YES;
    [self.byCategoryButton setBackgroundImage:[UIImage imageNamed:@"radioOff"]forState:UIControlStateNormal];
    [self.byCategoryButton setTitleColor:lightColor forState:UIControlStateNormal];
    [self.byCategoryButton setTitleColor:lightColor forState:UIControlStateHighlighted];

}

- (IBAction)byCategoryTapped:(UIButton *)sender {

    UIColor *darkColor = [UIColor colorWithRed:52/255 green:91/255 blue:120/255 alpha:1.0];
    UIColor *lightColor = [UIColor colorWithRed:158/255 green:200/255 blue:217/255 alpha:1.0];

    self.byDateView.hidden = YES;
    [self.byDateButton setBackgroundImage:[UIImage imageNamed:@"radioOff"] forState:UIControlStateNormal];
    [self.byDateButton setTitleColor:lightColor forState:UIControlStateNormal];
    [self.byDateButton setTitleColor:lightColor forState:UIControlStateHighlighted];

    self.byCategoryView.hidden = NO;
    [self.byCategoryButton setBackgroundImage:[UIImage imageNamed:@"radioOn"]forState:UIControlStateNormal];
    [self.byCategoryButton setTitleColor:darkColor forState:UIControlStateNormal];
    [self.byCategoryButton setTitleColor:darkColor forState:UIControlStateHighlighted];

}

The only thing that I can't seem to get to work is changing the button text color. Any ideas why?

Upvotes: 0

Views: 458

Answers (1)

Peter
Peter

Reputation: 1109

your IBAction methods should be as simple as

- (IBAction)byDateTapped:(UIButton *)sender {

[self.byDateButton setSelected:YES];
[self.byCategoryButton setSelected:NO];

}

- (IBAction)byCategoryTapped:(UIButton *)sender {

[self.byDateButton setSelected:NO];
[self.byCategoryButton setSelected:YES];

}

You should do all the setup for all the states on your initialisation code (viewDidLoad).

Instead of UIControlStateHighlighted, you should use UIControlStateSelected instead. FYI, You can also do the same thing for background image.

For example:

[_byDateButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_byDateButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
[_byCategoryButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_byCategoryButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];

Upvotes: 1

Related Questions