Tasked with building a UISegmentedControl with different colors for selected/unselected buttons, I created a subclass that accomplishes this by digging through the subviews of the segmented control. I can't verify that this will get approved by the mysterious app store process, nor can I say it will function properly if you're using all of the built-in functions of UISegmentedControl (adding/removing segments dynamically will break this code). But for a simple case, it's working well for my purposes. Here we go:
CustomSegmentedControl.h :
#import@interface CustomSegmentedControl : UISegmentedControl { UIColor *offColor; UIColor *onColor; BOOL hasSetSelectedIndexOnce; } -(id)initWithItems:(NSArray *)items offColor:(UIColor*)offcolor onColor:(UIColor*)oncolor; -(void)setInitialMode; -(void)setToggleHiliteColors; @end
CustomSegmentedControl.m :
#import "CustomSegmentedControl.h"
@implementation CustomSegmentedControl
-(id)initWithItems:(NSArray *)items offColor:(UIColor*)offcolor onColor:(UIColor*)oncolor {
if (self = [super initWithItems:items]) {
// Initialization code
offColor = [offcolor retain];
onColor = [oncolor retain];
hasSetSelectedIndexOnce = NO;
[self setInitialMode];
[self setSelectedSegmentIndex:0]; // default to first button, or the coloring gets all whacked out :(
}
return self;
}
-(void)setInitialMode
{
// set essential properties
[self setBackgroundColor:[UIColor clearColor]];
[self setSegmentedControlStyle:UISegmentedControlStyleBar];
// loop through children and set initial tint
for( int i = 0; i < [self.subviews count]; i++ )
{
[[self.subviews objectAtIndex:i] setTintColor:nil];
[[self.subviews objectAtIndex:i] setTintColor:offColor];
}
// listen for updates
[self addTarget:self action:@selector(setToggleHiliteColors) forControlEvents:UIControlEventValueChanged];
}
-(void)setToggleHiliteColors
{
// get current toggle nav index
int index = self.selectedSegmentIndex;
int numSegments = [self.subviews count];
for( int i = 0; i < numSegments; i++ )
{
// reset color
[[self.subviews objectAtIndex:i] setTintColor:nil];
[[self.subviews objectAtIndex:i] setTintColor:offColor];
}
if( hasSetSelectedIndexOnce )
{
// this is super weird - the subviews array is backwards... so deal with it like that
[[self.subviews objectAtIndex: numSegments - 1 - index] setTintColor:onColor];
}
else
{
// ...but the very first time, they're the expected order :-/
[[self.subviews objectAtIndex: index] setTintColor:onColor];
hasSetSelectedIndexOnce = YES;
}
}
@end
And to initialize :
NSArray *toggleItems = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil]; CustomSegmentedControl *toggleNav = [[CustomSegmentedControl alloc] initWithItems:toggleItems offColor:[UIColor blackColor] onColor:[UIColor redColor] ]; [toggleNav addTarget:self action:@selector(handleToggleNav:) forControlEvents:UIControlEventValueChanged]; [toggleNav setFrame:CGRectMake(52, 8, 211, 25)]; [self.view addSubview:toggleNav]; [toggleNav release];
Otherwise, follow the documentation for a UISegmentedControl, and enjoy.