UISegmentedControl Color in a UIToolbar
Asked Answered
D

1

9

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.

Dakar answered 1/1, 2010 at 20:56 Comment(3)
This helps: 'segmentedController.tintColor = [UIColor darkGrayColor];' but it creates a hard-coded dependency that I'd rather not introduce. For instance, making it slightly more complicated to allow user selection of color schemes.Dakar
Did you figure it out in the end?Reaves
Until Apple implements UINavigation coloring logic into the UIToolbar for the UISegmentedControl, I've been relegated to use the tintColor property.Dakar
P
7

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;
}
Pelligrini answered 3/1, 2010 at 10:4 Comment(6)
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.Dakar
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.Dakar
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.Pelligrini
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.Dakar
segmentedController.tintColor = [UIColor darkGrayColor]; solved my problem. Thanks.Apomixis
@LutherBaker In this case, try this approach. I am going do this in my app. 1) Use a singleton called AppTheme--which contains all theme settings. 2) When a view controller loads, in viewWillAppear set the tint color by reading from AppTheme. 3) Views off screen don't need to be changed, only when viewWillAppear hits does this need to happen. You can choose this simple approach, or go crazy and try to theme UIKit.Pelligrini

© 2022 - 2024 — McMap. All rights reserved.