Customize UISegmentedControl, add background image and selected segment tint color
Solution 1
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.
Solution 2
Use setBackgroundImage:forState:barMetrics:
with UIControlStateSelected
as the state.
Related videos on Youtube
Ameet Dhas
I have completed post graduation in Advanced computer science from university of Leicester. I am here to learn and contribute my knowledge.
Updated on September 15, 2022Comments
-
Ameet Dhas over 1 year
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]; } } }