A horizontal control made of multiple segments, each segment functioning as a discrete button.
- iOS 2.0+
- tvOS 9.0+
A segmented control can display a title (an
NSString object) or an image (
UIImage object). The
UISegmented 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
UIControl constant as shown below.
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.
In iOS 5 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,
[UISegmented), 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
In the case of the segmented control, appearance properties for
UIBar 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