UISegmentedControl is a class from UIKit in Apple iOS framework. A UISegmentedControl object is a horizontal control made of multiple segments, each segment functioning as a discrete button.
A UISegmentedControl object is a horizontal control made of multiple segments, each segment functioning as a discrete button. A segmented control affords a compact means to group together a number of controls.
A segmented control can display a title (an NSString object) or an image (UIImage object). The UISegmentedControl object automatically resizes segments to fit proportionally within their superview unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.
You register the target-action methods for a segmented control using the UIControlEventValueChanged constant as shown below.
SWIFT
segmentedControl.addTarget(self, action: "action:", forControlEvents: .ValueChanged);
OBJECTIVE-C
[segmentedControl addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventValueChanged];
How you configure a segmented control can affect its display behavior:
If you set a segmented control to have a momentary style, a segment doesn’t show itself as selected (blue background) when the user touches it. The disclosure button is always momentary and doesn’t affect the actual selection.
In versions of iOS prior to 3.0, if a segmented control has only two segments, then it behaves like a switch—tapping the currently-selected segment causes the other segment to be selected. On iOS 3.0 and later, tapping the currently-selected segment does not cause the other segment to be selected.
Customizing Appearance In iOS v5.0 and later, you can customize the appearance of segmented controls using the methods listed in Customizing Appearance. You can customize the appearance of all segmented controls using the appearance proxy (for example, [UISegmentedControl appearance]), or just of a single control.
When customizing appearance, in general, you should specify a value for the normal state of a property to be used by other states which don’t have a custom value set. Similarly, when a property is dependent on the bar metrics (on the iPhone in landscape orientation, bars have a different height from standard), you should make sure you specify a value for UIBarMetricsDefault.
In the case of the segmented control, appearance properties for UIBarMetricsLandscapePhone are only respected for segmented controls in the smaller navigation and toolbars that are used in landscape orientation on the iPhone.
To provide complete customization, you need to provide divider images for different state combinations, using setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics::
SWIFT
// Image between two unselected segments.
mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
// Image between segment selected on the left and unselected on the right.
mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Selected,
rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
// Image between segment selected on the right and unselected on the left.
mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
rightSegmentState: UIControlState.Selected, barMetrics: UIBarMetrics.Default)
OBJECTIVE-C
// Image between two unselected segments.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
// Image between segment selected on the left and unselected on the right.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
// Image between segment selected on the right and unselected on the right.
[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected barMetrics:barMetrics];
For more information about appearance and behavior configuration, see Segmented Controls.