How do you make the selected segment of a UISegmentedControl darker?

Go To StackoverFlow.com

3

I'm using UIAppearance to set global styles for my iOS app. I am envisioning a light gray style.

[[UINavigationBar appearance]
    setTintColor:[UIColor colorWithWhite:0.95 alpha 1.0]
];

[[UISegmentedControl appearance]
    setTintColor:[UIColor colorWithWhite:0.90 alpha 1.0]
];

enter image description here

The problem is that the selected segment (Uno) of the UISegmentedControl is not much darker than a normal segment (Dos). The normal segment is already at the right darkness, but I would like to only darken the selected segment, so people can tell the difference between the two. Darkening tint would darken both of them at the same time, so that won't work.

2012-04-04 04:21
by JoJo


1

The easiest way to do it is to iterate through a list of the subviews for a segmented controller and see which one is selected, when you find a selected subview you will need to adjust its tint color darker.

for (int x= 0; x <[aSegementedController.subviews count]; x++) 
{
    UIBarButtonItem *subview = [aSegementedController.subviews objectAtIndex:x];
    if ([subview isSelected]) 
    {               

        [subview setTintColor:darkerColor];
    }
}

This however doesn't work with UIAppearance, I do not believe it is customizable at that level.

2012-04-04 04:37
by Kyle Richter
I subclassed UISegmentedControl. From within this subclass, how can I listen for a change event and execute your sample code? I think it would be cleaner for the UISegmentedControl to handle its own styling, rather than the UIViewController listening for changes and then telling the UISegmentedControl to change its tint - JoJo 2012-04-04 05:18
Nevermind. I just realized that it is against model-view-controller paradigm for a view to understand events - JoJo 2012-04-04 05:31
Ads