views:

1328

answers:

3

Is there any way to customize color of selected segment in UISegmentedControl?

I've found segmentedController.tintColor property, which lets me customize color of the whole segmented control. The problem is, when I select bright color for tintColor property, selected segment becomes almost unrecognizable (its color is almost the same as the rest of segmented control, so its hard to distinguish selected and unselected segments). So I cannot use any good bright colors for segmented control. The solution would be some separate property for selected segment color but I cannot find it. Did anyone solve this?

+1  A: 

For doing your kind of thing, one might have to access the undocumented features and hacks, which will certainly make apple furious, and that may lead to the rejection of your application.

Now, the solution lies in other trick that you use two buttons instead and have their images interchanged when they are clicked. Keep the buttons closer and images of half segmented control to give the illusion of segmented control and that is all I can suggest you.

Hope this helps.

Thanks,

Madhup

Madhup
yeah, thanks, guess using UIButtons that look like segmented control is the only way to go if you want custom colors...
Mike
@Mike: I also think so. May be somebody suggest some other solution which is not a hack and uses the documented api indeed.
Madhup
+2  A: 

Hey, For some reason Apple dont allow you to change the color of standard UISegmentedControls.

There is however a "legal" way around it which is to change the segmented control style to UISegmentedControlStyleBar. This makes it look slightly different which you may not like but it does allow color.

    NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];

//Change Bar Style and ad to view then release segmented controller

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor colorWithRed:.9 green:.1 blue:.1 alpha:1]; 
[self.view addSubview:segmentedControl];
[segmentedControl release];

Hope this helped,

Seb Kade "I'm here to help"

Seb Kade
+1  A: 

Not sure if this will get approved by the app store, but I wrote a subclass to UISegmentedControl that lets you set a custom selected and unselected color. Check the notes for more info:

http://uihacker.blogspot.com/2010/05/iphone-uisegmentedcontrol-custom-colors.html

uihacker