开发者

UISegmentedControl Color in a UIToolbar

开发者 https://www.devze.com 2022-12-15 10:23 出处:网络
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 foll开发者_如何学JAVAows:

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 foll开发者_如何学JAVAows:

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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消