UISegmentedControl Color in a UIToolbar

Luther Baker picture Luther Baker · Jan 1, 2010 · Viewed 10.3k times · Source

My question revolves around the distinction of a UISegmentedController on a UINavigationBar vs a UIToolbar. If I drop a UISegmentedControl into a navigation bar as follows:

navigationBar.barStyle = UIBarStyleBlackTranslucent;

all is well. The UISegmentedControl identifies the selected option with a slightly darker black. But, if I drop a UISegmentedControl onto a UIToolbar, it doesn't pick up the color or translucency from the toolbar. If I manually set the tintColor the UISegmentedControl doesn't distinguish between selected and unselected anymore.

Admittedly, one must wrap the UISegmentedControl in a UIBarButtonItem before dropping onto a UIToolbar. I am wondering if that is part of the reason the UISegmentedControl looks incorrect (blue on a translucent black background).

toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; 
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:item,nil];
toolbar.items = toolbarItems;

Granted, my code is not EXACTLY as written since I am using the internal navigation and controller toolbar but the general logic is the same. I'm not sure how to make the UISegmentedControl on the UIToolbar have a black translucent style - maintaining an obvious distinction between selected and unselected segments.

Answer

bentford picture bentford · Jan 3, 2010

Seems like: segmentedController.tintColor = [UIColor darkGrayColor]; solves your problem.

To remove the "dependency", subclass UISegmentedControl and set the tint in the constructor.

CustomSegmentedControl.m

- (id)initWithItems:(NSArray*)items {
    if( self = [super initWithItems:items] ) {
         self.tintColor = [UIColor darkGrayColor];
    }
    return self;
}