Class

UISegmentedControl

A horizontal control made of multiple segments, each segment functioning as a discrete button.

Declaration

@interface UISegmentedControl : UIControl

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 UIControlEventValueChanged constant as shown below.

[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 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, [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::

// 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];

Topics

Initializing a Segmented Control

- initWithItems:

Initializes and returns a segmented control with segments having the given titles or images.

Managing Segment Content

- setImage:forSegmentAtIndex:

Sets the content of a segment to a given image.

- imageForSegmentAtIndex:

Returns the image for a specific segment

- setTitle:forSegmentAtIndex:

Sets the title of a segment.

- titleForSegmentAtIndex:

Returns the title of the specified segment.

Managing Segments

- insertSegmentWithImage:atIndex:animated:

Inserts a segment at a specified position in the receiver and gives it an image as content.

- insertSegmentWithTitle:atIndex:animated:

Inserts a segment at a specific position in the receiver and gives it a title as content.

numberOfSegments

Returns the number of segments the receiver has.

- removeAllSegments

Removes all segments of the receiver

- removeSegmentAtIndex:animated:

Removes the specified segment from the receiver, optionally animating the transition.

selectedSegmentIndex

The index number identifying the selected segment (that is, the last segment touched).

Managing Segment Behavior and Appearance

momentary

A Boolean value that determines whether segments in the receiver show selected state.

segmentedControlStyle

The style of the segmented control.

Deprecated
- setEnabled:forSegmentAtIndex:

Enables the specified segment.

- isEnabledForSegmentAtIndex:

Returns whether the indicated segment is enabled.

- setContentOffset:forSegmentAtIndex:

Adjusts the offset for drawing the content (image or text) of the specified segment.

- contentOffsetForSegmentAtIndex:

Returns the offset for drawing the content (image or text) of the specified segment.

- setWidth:forSegmentAtIndex:

Sets the width of the specified segment of the receiver.

- widthForSegmentAtIndex:

Returns the width of the indicated segment of the receiver.

apportionsSegmentWidthsByContent

Indicates whether the control attempts to adjust segment widths based on their content widths.

Customizing Appearance

- backgroundImageForState:barMetrics:

Returns the background image for a given state and bar metrics.

- setBackgroundImage:forState:barMetrics:

Sets the background image for a given state and bar metrics.

- contentPositionAdjustmentForSegmentType:barMetrics:

Returns the positioning offset for a given segment and bar metrics.

- setContentPositionAdjustment:forSegmentType:barMetrics:

Returns the content positioning offset for a given segment and bar metrics.

- dividerImageForLeftSegmentState:rightSegmentState:barMetrics:

Returns the divider image used for a given combination of left and right segment states and bar metrics.

- setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:

Sets the divider image used for a given combination of left and right segment states and bar metrics.

- titleTextAttributesForState:

Returns the text attributes of the title for a given control state.

- setTitleTextAttributes:forState:

Sets the text attributes of the title for a given control state.

Constants

UISegmentedControlStyle

The styles of the segmented control.

Deprecated
Segment Selection

A constant for indicating that no segment is selected.

UISegmentedControlSegment

Constants for specifying a segment in a control

Instance Properties

Relationships

Inherits From

Conforms To

See Also

Controls

UIControl

The base class for controls, which are visual elements that convey a specific action or intention in response to user interactions.

UIButton

A control that executes your custom code in response to user interactions.

UIDatePicker

A control used for the inputting of date and time values.

UIPageControl

A control that displays a horizontal series of dots, each of which corresponds to a page in the app’s document or other data-model entity.

UISlider

A control used to select a single value from a continuous range of values.

UIStepper

A control used to increment or decrement a value.

UISwitch

A control that offers a binary choice, such as On/Off.