Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of selected segment control

In my app,i able to change the color of the selected segment control.But the color is changed for another index rather than selected index. I can find any mistake in the index.

Help me!

my code is as follow:

if([SegmentRound selectedSegmentIndex] == 0)
    {

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor2];

        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];

        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor0];



        FLAGROUND=1;
    }

    if([SegmentRound selectedSegmentIndex] == 1)
    {
        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];

        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor0];

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor2];

        FLAGROUND=2;
    }
    if([SegmentRound selectedSegmentIndex] == 2)
    {
        UIColor *newSelectedTintColor0 = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:2] setTintColor:newSelectedTintColor0];

        UIColor *newSelectedTintColor2 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:0] setTintColor:newSelectedTintColor2];

        UIColor *newSelectedTintColor1 = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
        [[[SegmentRound subviews] objectAtIndex:1] setTintColor:newSelectedTintColor1];
        FLAGROUND=3;
    }

viewwillAppear:

[SegmentRound setSelectedSegmentIndex:0];
like image 335
piyush Avatar asked Aug 24 '12 06:08

piyush


People also ask

How do you change the segment control color?

To change the overall color of the segmented control use its backgroundColor . To change the color of the selected segment use selectedSegmentTintColor . To change the color/font of the unselected segment titles, use setTitleTextAttributes with a state of . normal / UIControlStateNormal .

How do I change text color in segmented control?

tintColor = [UIColor redColor]; for (id segment in [button subviews]) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { UILabel *titleLabel = (UILabel *) label; [titleLabel setTextColor:[UIColor blackColor]]; } } } UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] ...


2 Answers

I'd recommend to create the two colors outside of your condition, makes your code a bit smaller. Then you can use a foreach to iterate over your segments :

UIColor *selectedColor = [UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];

for (UIControl *subview in [SegmentRound subviews]) {
    if ([subview isSelected]) 
       [subview setTintColor:selectedColor]; 
    else
       [subview setTintColor:deselectedColor]; 
}
like image 113
Herm Avatar answered Sep 28 '22 09:09

Herm


check out this one

-(IBAction)segmentBtnPressed:(UISegmentedControl*)sender{
for (int i=0; i<[sender.subviews count]; i++) 
{
    if ([[sender.subviews objectAtIndex:i]isSelected] ) 
    {               
    UIColor *tintcolor=[UIColor colorWithRed: 98/255.0 green:156/255.0 blue:247/255.0 alpha:1.0];
    [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
    }
    else{
     UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
    [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];

    }
}
}

Also you can check out more answers here UISegmentedControl selected segment color

like image 37
Hiren Avatar answered Sep 28 '22 10:09

Hiren