Reputation: 2510
Dublicate of this, but its not working for me.
I have created UISegmentedControl using UICatalog and trying to change the selected segment color. I have used this to change color. The background image works fine but its not changing the selected segment color. What modifications should i have to do? Or any other approach for same? My code below.
NSArray *segmentTextContent = @[@"First",@"Second",@"Third"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.frame = CGRectMake(20, 50, 280, 30);
[segmentedControl addTarget:self
action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 1;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[segmentedControl setBackgroundImage:[UIImage imageNamed:@"navigationBar"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[segmentedControl setDividerImage:[UIImage imageNamed:@"divider"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
// we want attributed strings for this segmented control
NSDictionary *textAttributes = @{ UITextAttributeTextColor:[UIColor whiteColor],
UITextAttributeFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] };
[segmentedControl setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
textAttributes = @{ UITextAttributeTextColor:[UIColor whiteColor],
UITextAttributeFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] };
[segmentedControl setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
[self.view addSubview:segmentedControl];
- (void)segmentAction:(UISegmentedControl *)sender
{
for (int i=0; i<[sender.subviews count]; i++) {
if ([[sender.subviews objectAtIndex:i]isSelected]) {
UIColor *tintcolor = [UIColor greenColor];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
} else {
[[sender.subviews objectAtIndex:i] setTintColor:nil];
}
}
}
Upvotes: 3
Views: 12920
Reputation: 9423
In iOS 7 with the new behavior of the tintColor, try setting instead the color of the background. This will change the text color of the segmentedControl when it's selected.
Add this line before adding the segmentedControl to the view:
segmentedControl.backgroundColor = [UIColor greenColor];
So you don't need this anymore:
- (void)segmentAction:(UISegmentedControl *)sender
{
for (int i=0; i<[sender.subviews count]; i++) {
if ([[sender.subviews objectAtIndex:i]isSelected]) {
UIColor *tintcolor = [UIColor greenColor];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
} else {
[[sender.subviews objectAtIndex:i] setTintColor:nil];
}
}
}
Keep in mind that the background color of the unselected segmentedControl will change also. But if you have custom images, you will not see it.
Hope that helps.
Upvotes: 1
Reputation: 29
For UISegmentedControl you can use this code
for (int i=0; i<[sender.subviews count]; i++)
{
if ([[sender.subviews objectAtIndex:i]isSelected] )
{
UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
}
else
{
[[sender.subviews objectAtIndex:i] setTintColor:nil];
}
}
For background image
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
Upvotes: 0
Reputation: 113747
Use setBackgroundImage:forState:barMetrics:
with UIControlStateSelected
as the state.
Upvotes: 1