2.9. Customizing the UISegmentedControl
Problem
You have already placed a segmented control or two on your UI, and now want to be able to customize them to match your UI’s theme.
Solution
Either apply tint color to the segmented control or create your own images and apply them to this component.
Discussion
Tint colors are the easiest way of applying new colors to your UI
components. The UISegmentedControl
class has a property named tintColor
that you can utilize to change the tint color of your segmented control.
There is one important thing that you have to bear in mind before
attempting to change the value of the aforementioned property: the style
of your segmented control has to be set to UISegmentedControlStyleBar. You can change the
style of your segmented control by changing the value of the segmentedControlStyle property of your
control. Here is an example:
#import "ViewController.h"@interfaceViewController()@property(nonatomic,strong)UISegmentedControl*segmentedControl;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];NSArray*items=@[@"Item 1",@"Item 2",@"Item 3"];self.segmentedControl=[[UISegmentedControlalloc]initWithItems:items];/* We have to do this if we want to change the tint color */self.segmentedControl.segmentedControlStyle=UISegmentedControlStyleBar;[self.viewaddSubview:self.segmentedControl];self.segmentedControl.center=CGPointMake(self.view.center.x+25.0f,self.view.center.y);/* Change the tint color now */ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access