UISegmentedControl
A horizontal control that consists of multiple segments, each segment functioning as a discrete button.
Declaration
@MainActor class UISegmentedControlMentioned in
Overview
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 valueChanged 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. In iOS 3.0 and later, tapping the currently-selected segment doesn’t cause the other segment to be selected.
Customize appearance
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 UIBarMetrics.default.
In the case of the segmented control, appearance properties for landscapePhone 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:):
Topics
Creating a segmented control
Managing segment content
Managing segment actions
Managing segments
numberOfSegmentssegmentIndex(identifiedBy:)insertSegment(action:at:animated:)insertSegment(with:at:animated:)insertSegment(withTitle:at:animated:)removeAllSegments()removeSegment(at:animated:)selectedSegmentIndexnoSegment
Managing segment behavior and appearance
isMomentarysetEnabled(_:forSegmentAt:)isEnabledForSegment(at:)setContentOffset(_:forSegmentAt:)contentOffsetForSegment(at:)setWidth(_:forSegmentAt:)widthForSegment(at:)apportionsSegmentWidthsByContent
Customizing appearance
selectedSegmentTintColorbackgroundImage(for:barMetrics:)setBackgroundImage(_:for:barMetrics:)contentPositionAdjustment(forSegmentType:barMetrics:)setContentPositionAdjustment(_:forSegmentType:barMetrics:)UISegmentedControl.SegmentdividerImage(forLeftSegmentState:rightSegmentState:barMetrics:)setDividerImage(_:forLeftSegmentState:rightSegmentState:barMetrics:)titleTextAttributes(for:)setTitleTextAttributes(_:for:)