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"

@interface ViewController ()
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@end

@implementation ViewController

- (void)viewDidLoad{
    [super viewDidLoad];

    NSArray *items = @[@"Item 1", @"Item 2", @"Item 3"];
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
    /* We have to do this if we want to change the tint color */
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [self.view addSubview:self.segmentedControl];
    self.segmentedControl.center = CGPointMake(self.view.center.x + 25.0f,
                                               self.view.center.y);

    /* Change the tint color now */ ...

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.