问题
I have been wanting to apply different colors to my UISegmentedControl segments. Many people on here have been asking how to set tint color when you press on a certain segment. What i want to do is to set the tint color of each segment throughout the life of the application(or when the view appears on the screen).
Scanning the questions on here, different people have pointed out that evidently in iOS 6 you cannot set the tintColor of each segment as such:
- (void)viewDidLoad
{
[super viewDidLoad];
buttonNames = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", nil];
colorControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
[[[colorControl subviews] objectAtIndex:0] setTintColor:[UIColor redColor]];
[[[colorControl subviews] objectAtIndex:1] setTintColor:[UIColor greenColor]];
[[[colorControl subviews] objectAtIndex:2] setTintColor:[UIColor blueColor]];
colorControl.segmentedControlStyle = UISegmentedControlStyleBar;
colorControl.momentary = YES;
[colorControl addTarget:self action:@selector(colorSegmentSelected:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:colorControl];
}
I went and installed the iOS 5.0 and 5.1 simulators within XCode and set my project to target 5.0 and 5.1. In both cases this code would still not work. The tintColor of all 3 segments were gray.
- what is the proper way to set the tint color of each segment(if i have done it wrong here please correct me.
- If this code did work in iOS 5 how come that even when i target 5.0/5.1 and use the appropriate simulators it still doesnt work?
appreciate the feedback/knowledge on this subject. thanks!
来源:https://stackoverflow.com/questions/13827297/uisegmented-control-setting-the-tint-color-of-each-segment