Silver challenge - Setting the tintcolor for each segment


#1

I was trying to set up a way for my UISegmentedControl so that each segment would be a different color. I was searching around on stack overflow and came up with some previous examples such as :

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;

but quickly found out that this was no longer available in iOS 6.

I installed the 5.0 and 5.1 simulators in XCode and tried this code making sure my targets were 5.0/5.1 respectively and i still couldnt get it to work. All segments still have a grey background. I was trying to do more than just the challenge by coloring the background of each segment (tintColor) so thats how I made it down this rabbit hole. :smiley:

Would like to know why Apple does not allow for you to color each segment and if this code above isnt right then perhaps someone can correct me. Perhaps there is another way to make this happen and I just dont know how. Would like any feedback… this is whats in my viewDidLoad now…

[code]- (void)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];

}[/code]


#2

anyone got any ideas? Also i found something in the documentation where Apple contradicts themselves.

Under the UISegementedControl reference here:
developer.apple.com/library/ios/ … ntrol.html#//apple_ref/occ/instp/UISegmentedControl/tintColor it says

[quote]The default value of this property is nil (no color). UISegmentedControl uses this property only if the style of the segmented control is UISegmentedControlStyleBar.[/quote].

Yet if you go under the control styles section in this reference page look what i found:

[quote]UISegmentedControlStyleBezeled

The large bezeled style for segmented controls. Segmented controls in this style can have a tint color (see tintColor).

Available in iOS 4.0 and later.

Declared in UISegmentedControl.h.[/quote]

So can you use the property using only the ControlStyleBar or can you use it with the Bezeled control style? So far I have yet to determine what is the answer because i cannot even get the tintColor to work for iOS 5.0/5.1. If anyone has any insight please let me know… thanks…