You are on page 1of 29

UISegmentedControl Class Reference

Contents

UISegmentedControl Class Reference 4


Overview 4
Customizing Appearance 5
Tasks 6
Initializing a Segmented Control 6
Managing Segment Content 6
Managing Segments 6
Managing Segment Behavior and Appearance 7
Customizing Appearance 7
Properties 8
apportionsSegmentWidthsByContent 8
momentary 8
numberOfSegments 9
segmentedControlStyle 9
selectedSegmentIndex 9
tintColor 10
Instance Methods 11
backgroundImageForState:barMetrics: 11
contentOffsetForSegmentAtIndex: 11
contentPositionAdjustmentForSegmentType:barMetrics: 12
dividerImageForLeftSegmentState:rightSegmentState:barMetrics: 13
imageForSegmentAtIndex: 13
initWithItems: 14
insertSegmentWithImage:atIndex:animated: 14
insertSegmentWithTitle:atIndex:animated: 15
isEnabledForSegmentAtIndex: 16
removeAllSegments 16
removeSegmentAtIndex:animated: 17
setBackgroundImage:forState:barMetrics: 17
setContentOffset:forSegmentAtIndex: 18
setContentPositionAdjustment:forSegmentType:barMetrics: 19
setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics: 19
setEnabled:forSegmentAtIndex: 20
setImage:forSegmentAtIndex: 21

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

Contents

setTitle:forSegmentAtIndex: 21
setTitleTextAttributes:forState: 22
setWidth:forSegmentAtIndex: 22
titleForSegmentAtIndex: 23
titleTextAttributesForState: 24
widthForSegmentAtIndex: 24
Constants 25
UISegmentedControlStyle 25
Segment Selection 26
UISegmentedControlSegment 26

Document Revision History 28

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference

Inherits from

UIControl : UIView : UIResponder : NSObject

Conforms to

NSCoding
NSCoding (UIView)
UIAppearance (UIView)
UIAppearanceContainer (UIView)
NSObject (NSObject)

Framework

/System/Library/Frameworks/UIKit.framework

Availability

Available in iOS 2.0 and later.

Declared in

UISegmentedControl.h

Related sample code

iPhoneACFileConvertTest
iPhoneExtAudioFileConvertTest
NavBar
Quartz2D for iOS
UICatalog

Overview
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.
[segmentedControl addTarget:self

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference


Overview

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 doesnt show itself as selected (blue
background) when the user touches it. The disclosure button is always momentary and doesnt 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
switchtapping 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 (page 7). 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 dont 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: (page 19):
// 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];

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference


Tasks

Tasks
Initializing a Segmented Control
(page 14)
Initializes and returns a segmented control with segments having the given titles or images.

initWithItems:

Managing Segment Content


(page 21)
Sets the content of a segment to a given image.

setImage:forSegmentAtIndex:

(page 13)
Returns the image for a specific segment

imageForSegmentAtIndex:

setTitle:forSegmentAtIndex:

(page 21)

Sets the title of a segment.


(page 23)
Returns the title of the specified segment.

titleForSegmentAtIndex:

Managing Segments
(page 14)
Inserts a segment at a specified position in the receiver and gives it an image as content.

insertSegmentWithImage:atIndex:animated:

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

insertSegmentWithTitle:atIndex:animated:

(page 9) property
Returns the number of segments the receiver has. (read-only)

numberOfSegments

(page 16)
Removes all segments of the receiver

removeAllSegments

(page 17)
Removes the specified segment from the receiver, optionally animating the transition.

removeSegmentAtIndex:animated:

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

selectedSegmentIndex

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference


Tasks

Managing Segment Behavior and Appearance


(page 8) property
A Boolean value that determines whether segments in the receiver show selected state.

momentary

(page 9) property
The style of the segmented control.

segmentedControlStyle

setEnabled:forSegmentAtIndex:

(page 20)

Enables the specified segment.


(page 16)
Returns whether the indicated segment is enabled.

isEnabledForSegmentAtIndex:

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

setContentOffset:forSegmentAtIndex:

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

contentOffsetForSegmentAtIndex:

(page 22)
Sets the width of the specified segment of the receiver.

setWidth:forSegmentAtIndex:

(page 24)
Returns the width of the indicated segment of the receiver.

widthForSegmentAtIndex:

(page 8) property
Indicates whether the control attempts to adjust segment widths based on their content widths.

apportionsSegmentWidthsByContent

Customizing Appearance
(page 10) property
The tint color of the segmented control.

tintColor

(page 11)
Returns the background image for a given state and bar metrics.

backgroundImageForState:barMetrics:

(page 17)
Sets the background image for a given state and bar metrics.

setBackgroundImage:forState:barMetrics:

(page 12)
Returns the positioning offset for a given segment and bar metrics.

contentPositionAdjustmentForSegmentType:barMetrics:

(page 19)
Returns the content positioning offset for a given segment and bar metrics.

setContentPositionAdjustment:forSegmentType:barMetrics:

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

dividerImageForLeftSegmentState:rightSegmentState:barMetrics:

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference


Properties

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

setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:

(page 24)
Returns the text attributes of the title for a given control state.

titleTextAttributesForState:

(page 22)
Sets the text attributes of the title for a given control state.

setTitleTextAttributes:forState:

Properties
apportionsSegmentWidthsByContent
Indicates whether the control attempts to adjust segment widths based on their content widths.
@property(nonatomic) BOOL apportionsSegmentWidthsByContent

Discussion
If the value of this property is YES, for segments whose width value is 0, the control attempts to adjust segment
widths based on their content widths.
The default is NO.
Availability
Available in iOS 5.0 and later.
Declared in
UISegmentedControl.h

momentary
A Boolean value that determines whether segments in the receiver show selected state.
@property(nonatomic, getter=isMomentary) BOOL momentary

Discussion
The default value of this property is NO. If it is set to YES, segments in the control do not show selected state
and do not update the value of selectedSegmentIndex (page 9) after tracking ends.
Availability
Available in iOS 2.0 and later.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference


Properties

Related Sample Code

NavBar

Declared in
UISegmentedControl.h

numberOfSegments
Returns the number of segments the receiver has. (read-only)
@property(nonatomic, readonly) NSUInteger numberOfSegments

Availability
Available in iOS 2.0 and later.
Declared in
UISegmentedControl.h

segmentedControlStyle
The style of the segmented control.
@property(nonatomic) UISegmentedControlStyle segmentedControlStyle

Discussion
The default style is UISegmentedControlStylePlain (page 25). See UISegmentedControlStyle (page 25)
for descriptions of valid constants.
Availability
Available in iOS 2.0 and later.
Related Sample Code

GLPaint
NavBar
UICatalog

Declared in
UISegmentedControl.h

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

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

UISegmentedControl Class Reference


Properties

@property(nonatomic) NSInteger selectedSegmentIndex

Discussion
The default value is UISegmentedControlNoSegment (page 26) (no segment selected) until the user touches
a segment. Set this property to -1 to turn off the current selection. UISegmentedControl ignores this property
when the control is in momentary mode. When the user touches a segment to change the selection, the control
event UIControlEventValueChanged is generated; if the segmented control is set up to respond to this
control event, it sends a action message to its target.
Availability
Available in iOS 2.0 and later.
See Also
@property momentary

(page 8)

Related Sample Code

iPhoneCoreDataRecipes
NavBar
Quartz2D for iOS
State Restoration of Child View Controllers
UICatalog
Declared in
UISegmentedControl.h

tintColor
The tint color of the segmented control.
@property(nonatomic, retain) UIColor *tintColor

Discussion
The default value of this property is nil (no color). UISegmentedControl uses this property only if the style
of the segmented control is UISegmentedControlStyleBar (page 25).
Availability
Available in iOS 2.0 and later.
Related Sample Code

GLPaint
NavBar
PVRTextureLoader
UICatalog

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

10

UISegmentedControl Class Reference


Instance Methods

Declared in
UISegmentedControl.h

Instance Methods
backgroundImageForState:barMetrics:
Returns the background image for a given state and bar metrics.
- (UIImage *)backgroundImageForState:(UIControlState)state

barMetrics:(UIBarMetrics)barMetrics

Parameters
state

A control state.
barMetrics

Bar metrics.
Return Value
The background image for state and barMetrics.
Discussion
For more details, see setBackgroundImage:forState:barMetrics: (page 17).
Availability
Available in iOS 5.0 and later.
Declared in
UISegmentedControl.h

contentOffsetForSegmentAtIndex:
Returns the offset for drawing the content (image or text) of the specified segment.
- (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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

11

UISegmentedControl Class Reference


Instance Methods

Return Value
The offset (specified as a CGSize structure) from the origin of the segment at which to draw the segments
content.
Availability
Available in iOS 2.0 and later.
See Also
setContentOffset:forSegmentAtIndex:

(page 18)

Declared in
UISegmentedControl.h

contentPositionAdjustmentForSegmentType:barMetrics:
Returns the positioning offset for a given segment and bar metrics.
(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone

barMetrics:(UIBarMetrics)barMetrics

Parameters
leftCenterRightOrAlone

An identifier for a segment.


barMetrics

Bar metrics.
Return Value
The content positioning offset for the segment identified by leftCenterRightOrAlone and barMetrics.
Discussion
For more details, see setContentPositionAdjustment:forSegmentType:barMetrics: (page 19).
Availability
Available in iOS 5.0 and later.
See Also
setContentPositionAdjustment:forSegmentType:barMetrics:

(page 19)

Declared in
UISegmentedControl.h

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

12

UISegmentedControl Class Reference


Instance Methods

dividerImageForLeftSegmentState:rightSegmentState:barMetrics:
Returns the divider image used for a given combination of left and right segment states and bar metrics.
- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftState

rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

Parameters
leftState

The state of the left segment.


rightState

The state of the right segment.


barMetrics

Bar metrics.
Return Value
The divider image used for the given combination of left and right segment states and bar metrics
Availability
Available in iOS 5.0 and later.
Declared in
UISegmentedControl.h

imageForSegmentAtIndex:
Returns the image for a specific segment
- (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 (page 9)) 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.
Availability
Available in iOS 2.0 and later.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

13

UISegmentedControl Class Reference


Instance Methods

See Also
setImage:forSegmentAtIndex:

(page 21)

Related Sample Code

UICatalog

Declared in
UISegmentedControl.h

initWithItems:
Initializes and returns a segmented control with segments having the given titles or images.
- (id)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.
Availability
Available in iOS 2.0 and later.
Declared in
UISegmentedControl.h

insertSegmentWithImage:atIndex:animated:
Inserts a segment at a specified position in the receiver and gives it an image as content.
- (void)insertSegmentWithImage:(UIImage *)image atIndex:(NSUInteger)segment

animated:(BOOL)animated

Parameters
image

An image object to use as the content of the segment.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

14

UISegmentedControl Class Reference


Instance Methods

segment

An index number identifying a segment in the control.


segment must be a number in the range 0 to the number of segments (numberOfSegments (page 9))

inclusive; values exceeding this upper range are pinned to it.


The new segment is inserted just before the designated one.
animated
YES if the insertion of the new segment should be animated, otherwise NO.

Availability
Available in iOS 2.0 and later.
See Also
insertSegmentWithTitle:atIndex:animated:
removeSegmentAtIndex:animated:

(page 15)

(page 17)

Declared in
UISegmentedControl.h

insertSegmentWithTitle:atIndex:animated:
Inserts a segment at a specific position in the receiver and gives it a title as content.
- (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment

animated:(BOOL)animated

Parameters
title

A string to use as the segments title.


segment

An index number identifying a segment in the control.


segment must be a number in the range 0 to the number of segments (numberOfSegments (page 9))

inclusive; values exceeding this upper range are pinned to it.


The new segment is inserted just before the designated one.
animated
YES if the insertion of the new segment should be animated, otherwise NO.

Availability
Available in iOS 2.0 and later.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

15

UISegmentedControl Class Reference


Instance Methods

See Also
insertSegmentWithImage:atIndex:animated:
removeSegmentAtIndex:animated:

(page 14)

(page 17)

Declared in
UISegmentedControl.h

isEnabledForSegmentAtIndex:
Returns whether the indicated segment is enabled.
- (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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.
Return Value
YES if the given segment is enabled and NO if the segment is disabled. By default, segments are enabled.
Availability
Available in iOS 2.0 and later.
See Also
setEnabled:forSegmentAtIndex:

(page 20)

Declared in
UISegmentedControl.h

removeAllSegments
Removes all segments of the receiver
- (void)removeAllSegments

Availability
Available in iOS 2.0 and later.
See Also
removeSegmentAtIndex:animated:

(page 17)

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

16

UISegmentedControl Class Reference


Instance Methods

Declared in
UISegmentedControl.h

removeSegmentAtIndex:animated:
Removes the specified segment from the receiver, optionally animating the transition.
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated

Parameters
segment

An index number identifying a segment in the control. It must be a number between 0 and the number
of segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned to
it.
animated
YES if the removal of the new segment should be animated, otherwise NO.

Availability
Available in iOS 2.0 and later.
See Also
removeAllSegments

(page 16)

insertSegmentWithImage:atIndex:animated:
insertSegmentWithTitle:atIndex:animated:

(page 14)
(page 15)

Declared in
UISegmentedControl.h

setBackgroundImage:forState:barMetrics:
Sets the background image for a given state and bar metrics.
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state

barMetrics:(UIBarMetrics)barMetrics

Parameters
backgroundImage

The background image to use for state and barMetrics.


state

A control state.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

17

UISegmentedControl Class Reference


Instance Methods

barMetrics

Bar metrics.
Discussion
If backgroundImage is an image returned from resizableImageWithCapInsets:, the cap widths are
calculated from that information.
If backgroundImage is not an image returned from resizableImageWithCapInsets:, the cap width is
calculated by subtracting one from the image's width then dividing by 2. The cap widths are used as the
margins for text placement. To adjust the margin, use the margin adjustment methods.
Discussion
For more details, see backgroundImageForState:barMetrics: (page 11).
Availability
Available in iOS 5.0 and later.
Related Sample Code

UICatalog

Declared in
UISegmentedControl.h

setContentOffset:forSegmentAtIndex:
Adjusts the offset for drawing the content (image or text) of the specified segment.
- (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 segments
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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.
Availability
Available in iOS 2.0 and later.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

18

UISegmentedControl Class Reference


Instance Methods

See Also
contentOffsetForSegmentAtIndex:

(page 11)

Declared in
UISegmentedControl.h

setContentPositionAdjustment:forSegmentType:barMetrics:
Returns the content positioning offset for a given segment and bar metrics.
- (void)setContentPositionAdjustment:(UIOffset)adjustment

forSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlone
barMetrics:(UIBarMetrics)barMetrics

Parameters
adjustment

The positioning offset for the segment identified by leftCenterRightOrAlone and barMetrics.
leftCenterRightOrAlone

An identifier for a segment type.


barMetrics

Bar metrics.
Discussion
You use this method to adjust the position of a title or image within the given segment of a segmented control.
Availability
Available in iOS 5.0 and later.
See Also
contentPositionAdjustmentForSegmentType:barMetrics:

(page 12)

Declared in
UISegmentedControl.h

setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:
Sets the divider image used for a given combination of left and right segment states and bar metrics.
- (void)setDividerImage:(UIImage *)dividerImage

forLeftSegmentState:(UIControlState)leftState
rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

19

UISegmentedControl Class Reference


Instance Methods

Parameters
dividerImage

The divider image to use.


leftState

The state of the left segment.


rightState

The state of the right segment.


barMetrics

Bar metrics.
Availability
Available in iOS 5.0 and later.
Related Sample Code

UICatalog

Declared in
UISegmentedControl.h

setEnabled:forSegmentAtIndex:
Enables the specified segment.
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment

Parameters
enabled
YES to enable the specified segment or NO 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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.
Availability
Available in iOS 2.0 and later.
See Also
isEnabledForSegmentAtIndex:

(page 16)

Declared in
UISegmentedControl.h

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

20

UISegmentedControl Class Reference


Instance Methods

setImage:forSegmentAtIndex:
Sets the content of a segment to a given image.
- (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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.
Discussion
A segment can only have an image or a title; it cant have both. There is no default image.
Availability
Available in iOS 2.0 and later.
See Also
imageForSegmentAtIndex:

(page 13)

Declared in
UISegmentedControl.h

setTitle:forSegmentAtIndex:
Sets the title of a segment.
- (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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.
Discussion
A segment can only have an image or a title; it cant have both. There is no default title.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

21

UISegmentedControl Class Reference


Instance Methods

Availability
Available in iOS 2.0 and later.
See Also
titleForSegmentAtIndex:

(page 23)

Declared in
UISegmentedControl.h

setTitleTextAttributes:forState:
Sets the text attributes of the title for a given control state.
- (void)setTitleTextAttributes:(NSDictionary *)attributes

forState:(UIControlState)state

Parameters
attributes

The text attributes of the title for state.


state

A control state.
Discussion
The attributes dictionary can specify the font, text color, text shadow color, and text shadow offset for the title
in the text attributes dictionary, using the keys in NSString UIKit Additions Reference .
Availability
Available in iOS 5.0 and later.
Related Sample Code

UICatalog

Declared in
UISegmentedControl.h

setWidth:forSegmentAtIndex:
Sets the width of the specified segment of the receiver.
- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

22

UISegmentedControl Class Reference


Instance Methods

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 (page 9)) minus 1; values exceeding this upper range are pinned to
it.
Availability
Available in iOS 2.0 and later.
See Also
widthForSegmentAtIndex:

(page 24)

Declared in
UISegmentedControl.h

titleForSegmentAtIndex:
Returns the title of the specified segment.
- (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 (page 9)) 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.
Availability
Available in iOS 2.0 and later.
See Also
setTitle:forSegmentAtIndex:

(page 21)

Declared in
UISegmentedControl.h

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

23

UISegmentedControl Class Reference


Instance Methods

titleTextAttributesForState:
Returns the text attributes of the title for a given control state.
- (NSDictionary *)titleTextAttributesForState:(UIControlState)state

Parameters
state

A control state.
Return Value
The text attributes of the title for state.
Discussion
For more details, see setTitleTextAttributes:forState: (page 22)
Availability
Available in iOS 5.0 and later.
Declared in
UISegmentedControl.h

widthForSegmentAtIndex:
Returns the width of the indicated segment of the receiver.
- (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 (page 9)) 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.
Availability
Available in iOS 2.0 and later.
See Also
setWidth:forSegmentAtIndex:

(page 22)

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

24

UISegmentedControl Class Reference


Constants

Declared in
UISegmentedControl.h

Constants
UISegmentedControlStyle
The styles of the segmented control.

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

Constants
UISegmentedControlStylePlain

The large plain style for segmented controls. This style is the default.
Available in iOS 2.0 and later.
Declared in UISegmentedControl.h.
UISegmentedControlStyleBordered

The large bordered style for segmented controls.


Available in iOS 2.0 and later.
Declared in UISegmentedControl.h.
UISegmentedControlStyleBar

The small toolbar style for segmented controls. Segmented controls in this style can have a tint color (see
tintColor (page 10)).
Available in iOS 2.0 and later.
Declared in UISegmentedControl.h.
UISegmentedControlStyleBezeled

The large bezeled style for segmented controls. Segmented controls in this style can have a tint color
(see tintColor (page 10)).
Available in iOS 4.0 and later.
Declared in UISegmentedControl.h.
Discussion
You use these constants as values for the segmentedControlStyle (page 9) property.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

25

UISegmentedControl Class Reference


Constants

Segment Selection
A constant for indicating that no segment is selected.

enum {
UISegmentedControlNoSegment = -1
};

Constants
UISegmentedControlNoSegment

A segment index value indicating that there is no selected segment. See selectedSegmentIndex (page
9) for further information.
Available in iOS 2.0 and later.
Declared in UISegmentedControl.h.

UISegmentedControlSegment
Constants for specifying a segment in a control

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

Constants
UISegmentedControlSegmentAny

Specifies any segment.


Available in iOS 5.0 and later.
Declared in UISegmentedControl.h.
UISegmentedControlSegmentLeft

The capped, leftmost segment.


Only applies when numSegments > 1.
Available in iOS 5.0 and later.
Declared in UISegmentedControl.h.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

26

UISegmentedControl Class Reference


Constants

UISegmentedControlSegmentCenter

Any segment between the left and rightmost segments.


Only applies when numSegments > 2.
Available in iOS 5.0 and later.
Declared in UISegmentedControl.h.
UISegmentedControlSegmentRight

The capped,rightmost segment. .


Only applies when numSegments > 1.
Available in iOS 5.0 and later.
Declared in UISegmentedControl.h.
UISegmentedControlSegmentAlone

The standalone segment, capped on both ends.


Only applies when numSegments = 1.
Available in iOS 5.0 and later.
Declared in UISegmentedControl.h.

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

27

Document Revision History

This table describes the changes to UISegmentedControl Class Reference .

Date

Notes

2011-10-12

Updated for iOS v5.0.

2010-05-17

Added the UISegmentedControlStyleBezeled constant.

2010-03-23

Fixed incorrect see also links.

2009-03-06

Updated for iOS 3.0.

2008-09-30

Corrected description of selectedSegmentIndex property.

2008-06-10

New document that describes the class implementing horizontal controls


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

2011-10-12 | Copyright 2011 Apple Inc. All Rights Reserved.

28

Apple Inc.
Copyright 2011 Apple Inc.
All rights reserved.
No part of this publication may be reproduced,
stored in a retrieval system, or transmitted, in any
form or by any means, mechanical, electronic,
photocopying, recording, or otherwise, without
prior written permission of Apple Inc., with the
following exceptions: Any person is hereby
authorized to store documentation on a single
computer for personal use only and to print
copies of documentation for personal use
provided that the documentation contains
Apples copyright notice.
No licenses, express or implied, are granted with
respect to any of the technology described in this
document. Apple retains all intellectual property
rights associated with the technology described
in this document. This document is intended to
assist application developers to develop
applications only for Apple-labeled computers.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
Apple, the Apple logo, iPhone, and Quartz are
trademarks of Apple Inc., registered in the U.S.
and other countries.
iOS is a trademark or registered trademark of
Cisco in the U.S. and other countries and is used
under license.
Even though Apple has reviewed this document,
APPLE MAKES NO WARRANTY OR REPRESENTATION,
EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS
DOCUMENT, ITS QUALITY, ACCURACY,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR
PURPOSE. AS A RESULT, THIS DOCUMENT IS PROVIDED
AS IS, AND YOU, THE READER, ARE ASSUMING THE
ENTIRE RISK AS TO ITS QUALITY AND ACCURACY.
IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES RESULTING FROM ANY DEFECT OR
INACCURACY IN THIS DOCUMENT, even if advised of
the possibility of such damages.
THE WARRANTY AND REMEDIES SET FORTH ABOVE
ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL
OR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer,
agent, or employee is authorized to make any
modification, extension, or addition to this warranty.
Some states do not allow the exclusion or limitation
of implied warranties or liability for incidental or
consequential damages, so the above limitation or
exclusion may not apply to you. This warranty gives
you specific legal rights, and you may also have other
rights which vary from state to state.

You might also like