views:

2956

answers:

1

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.

+1  A: 

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;
}
bentford
I think you're just moving the dependency, not getting rid of it. For instance, if I change the toolbar color to "green" - I'd have to go and find every line of code like this and change it. I'd rather not set my app up like that. I want the segmented bar to "automatically" feed off of it's parent. Indeed, it does this automatically when placed in a UINavigationController. If you change the UNavigationBar style to translucent black - any child UISegmentedController automatically follows suite.
Luther Baker
Unfortunately, when a UISegmentedControl is placed inside of a UIToolbar, it doesn't automatically reflect the toolbar's style/color option and, as you've illustrated, I've got to explicitly tell it what color to be. In this example, I'm identifying that as a "dependency" and I'm looking for a solution that doesn't introduce that type of dependency. I don't want to set the color in multiple places.
Luther Baker
Because this is a subclass, the color only occurs once in your code. This is a feature of object-oriented design. You can have 100 CustomSegmentedControl instances in you app, but the color is only indicated once.
bentford
It is more than that. When I allow the user to dynamically change the background color scheme ... all 100 of your CustomSegmentedControl instances on the screen are going to be the wrong color. Your approach requires that I remember, track and update each instance of any control I've custom colored. I'm looking for something a bit more elegant ... like the built in behavior I describe above where the UISegmentedControl automatically turns blue or black or translucent based on the UINavigationBar's style. Unfortunately, it didn't seem to do that on the UIToolbar.
Luther Baker
@bentford Thanks, it worked perfectly.
gerry3