iOS Developer Library

Developer

UIKit Framework Reference UISegmentedControl Class Reference

Options
Deployment Target:

On This Page
Language:

UISegmentedControl

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. More...

Inheritance


Import Statement


import UIKit @import UIKit;

Availability


Available in iOS 2.0 and later.
  • Initializes and returns a segmented control with segments having the given titles or images.

    Declaration

    Swift

    init(items items: [AnyObject]!)

    Objective-C

    - (instancetype)initWithItems:(NSArray *)items

    Parameters

    items

    An array of NSString objects (for segment titles) or UIImage objects (for segment images).

    Return Value

    A UISegmentedControl object or nil if there was a problem in initializing the object.

    Discussion

    The returned segmented control is automatically sized to fit its content within the width of its superview.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sets the content of a segment to a given image.

    Declaration

    Swift

    func setImage(_ image: UIImage?, forSegmentAtIndex segment: Int)

    Objective-C

    - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment

    Parameters

    image

    An image object to display in the segment. .

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Discussion

    A segment can only have an image or a title; it can’t have both. There is no default image.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the image for a specific segment

    Declaration

    Swift

    func imageForSegmentAtIndex(_ segment: Int) -> UIImage?

    Objective-C

    - (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment

    Parameters

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Return Value

    Returns the image assigned to the receiver as content. If no image has been set, it returns nil.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sets the title of a segment.

    Declaration

    Swift

    func setTitle(_ title: String?, forSegmentAtIndex segment: Int)

    Objective-C

    - (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment

    Parameters

    title

    A string to display in the segment as its title.

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Discussion

    A segment can only have an image or a title; it can’t have both. There is no default title.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the title of the specified segment.

    Declaration

    Swift

    func titleForSegmentAtIndex(_ segment: Int) -> String?

    Objective-C

    - (NSString *)titleForSegmentAtIndex:(NSUInteger)segment

    Parameters

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Return Value

    Returns the string (title) assigned to the receiver as content. If no title has been set, it returns nil.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • momentary momentary Property

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

    Declaration

    Swift

    var momentary: Bool

    Objective-C

    @property(nonatomic, getter=isMomentary) BOOL momentary

    Discussion

    The default value of this property is NOfalse. If it is set to YEStrue, segments in the control do not show selected state and do not update the value of selectedSegmentIndex after tracking ends.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • The style of the segmented control.

    Deprecation Statement

    Segmented controls no longer support multiple styles using this functionality. Use the other appearance modification methods and properties to customize a segmented control.

    Declaration

    Objective-C

    @property(nonatomic) UISegmentedControlStyle segmentedControlStyle

    Discussion

    The default style is UISegmentedControlStylePlain. See UISegmentedControlStyle for descriptions of valid constants.

    Import Statement

    Availability

    Available in iOS 2.0 and later.

    Deprecated in iOS 7.0.

  • Enables the specified segment.

    Declaration

    Swift

    func setEnabled(_ enabled: Bool, forSegmentAtIndex segment: Int)

    Objective-C

    - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment

    Parameters

    enabled

    YEStrue to enable the specified segment or NOfalse to disable the segment. By default, segments are enabled.

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns whether the indicated segment is enabled.

    Declaration

    Swift

    func isEnabledForSegmentAtIndex(_ segment: Int) -> Bool

    Objective-C

    - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment

    Parameters

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Return Value

    YEStrue if the given segment is enabled and NOfalse if the segment is disabled. By default, segments are enabled.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func setContentOffset(_ offset: CGSize, forSegmentAtIndex segment: Int)

    Objective-C

    - (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment

    Parameters

    offset

    The offset (specified as a CGSize type) from the origin of the segment at which to draw the segment’s content. The default offset is (0,0).

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func contentOffsetForSegmentAtIndex(_ segment: Int) -> CGSize

    Objective-C

    - (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment

    Parameters

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Return Value

    The offset (specified as a CGSize structure) from the origin of the segment at which to draw the segment’s content.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Sets the width of the specified segment of the receiver.

    Declaration

    Swift

    func setWidth(_ width: CGFloat, forSegmentAtIndex segment: Int)

    Objective-C

    - (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment

    Parameters

    width

    A float value specifying the width of the segment. The default value is {0.0}, which tells UISegmentedControl to automatically size the segment.

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Returns the width of the indicated segment of the receiver.

    Declaration

    Swift

    func widthForSegmentAtIndex(_ segment: Int) -> CGFloat

    Objective-C

    - (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment

    Parameters

    segment

    An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.

    Return Value

    A float value specifying the width of the segment. If the value is {0.0}, UISegmentedControl automatically sizes the segment.

    Import Statement

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    var apportionsSegmentWidthsByContent: Bool

    Objective-C

    @property(nonatomic) BOOL apportionsSegmentWidthsByContent

    Discussion

    If the value of this property is YEStrue, for segments whose width value is 0, the control attempts to adjust segment widths based on their content widths.

    The default is NOfalse.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • The styles of the segmented control.

    Use tintColor instead.

    Declaration

    Objective-C

    typedef enum { UISegmentedControlStylePlain, UISegmentedControlStyleBordered, UISegmentedControlStyleBar, UISegmentedControlStyleBezeled, } UISegmentedControlStyle;

    Constants

    • UISegmentedControlStylePlain

      UISegmentedControlStylePlain

      The large plain style for segmented controls. This style is the default.

      Available in iOS 2.0 and later.

      Deprecated in iOS 7.0.

    • UISegmentedControlStyleBordered

      UISegmentedControlStyleBordered

      The large bordered style for segmented controls.

      Available in iOS 2.0 and later.

      Deprecated in iOS 7.0.

    • UISegmentedControlStyleBar

      UISegmentedControlStyleBar

      The small toolbar style for segmented controls. Segmented controls in this style can have a tint color (see tintColor).

      Available in iOS 2.0 and later.

      Deprecated in iOS 7.0.

    • UISegmentedControlStyleBezeled

      UISegmentedControlStyleBezeled

      The large bezeled style for segmented controls. Segmented controls in this style can have a tint color (see tintColor).

      This control style was deprecated in iOS 5.1 and should not be used.

      Available in iOS 2.0 and later.

      Deprecated in iOS 7.0.

    Discussion

    You use these constants as values for the segmentedControlStyle property.

    Import Statement

    Availability

    Available in iOS 2.0 and later.

    Deprecated in iOS 7.0.

  • A constant for indicating that no segment is selected.

    Declaration

    Swift

    var UISegmentedControlNoSegment: Int { get }

    Objective-C

    enum { UISegmentedControlNoSegment = -1 };

    Constants

    • UISegmentedControlNoSegment

      UISegmentedControlNoSegment

      A segment index value indicating that there is no selected segment. See selectedSegmentIndex for further information.

      Available in iOS 2.0 and later.

    Import Statement

  • Constants for specifying a segment in a control

    Declaration

    Swift

    enum UISegmentedControlSegment : Int { case Any case Left case Center case Right case Alone }

    Objective-C

    typedef enum { UISegmentedControlSegmentAny = 0, UISegmentedControlSegmentLeft = 1, UISegmentedControlSegmentCenter = 2, UISegmentedControlSegmentRight = 3, UISegmentedControlSegmentAlone = 4, } UISegmentedControlSegment;

    Constants

    • Any

      UISegmentedControlSegmentAny

      Specifies any segment.

      Available in iOS 5.0 and later.

    • Left

      UISegmentedControlSegmentLeft

      The capped, leftmost segment.

      Only applies when numSegments > 1.

      Available in iOS 5.0 and later.

    • Center

      UISegmentedControlSegmentCenter

      Any segment between the left and rightmost segments.

      Only applies when numSegments > 2.

      Available in iOS 5.0 and later.

    • Right

      UISegmentedControlSegmentRight

      The capped,rightmost segment. .

      Only applies when numSegments > 1.

      Available in iOS 5.0 and later.

    • Alone

      UISegmentedControlSegmentAlone

      The standalone segment, capped on both ends.

      Only applies when numSegments = 1.

      Available in iOS 5.0 and later.

    Import Statement

    import UIKit

    Availability

    Available in iOS 5.0 and later.