UISegmentedControl Class

30
UISegmentedControl Class Reference

description

io development ui segmet control

Transcript of UISegmentedControl Class

Page 1: UISegmentedControl Class

UISegmentedControl Class Reference

Page 2: UISegmentedControl Class

Contents

UISegmentedControl Class Reference 4Overview 4

Customizing Appearance 5Tasks 6

Initializing a Segmented Control 6Managing Segment Content 6Managing Segments 6Managing Segment Behavior and Appearance 7Customizing Appearance 7

Properties 8apportionsSegmentWidthsByContent 8momentary 9numberOfSegments 9selectedSegmentIndex 9tintColor 10

Instance Methods 11backgroundImageForState:barMetrics: 11contentOffsetForSegmentAtIndex: 11contentPositionAdjustmentForSegmentType:barMetrics: 12dividerImageForLeftSegmentState:rightSegmentState:barMetrics: 12imageForSegmentAtIndex: 13initWithItems: 14insertSegmentWithImage:atIndex:animated: 14insertSegmentWithTitle:atIndex:animated: 15isEnabledForSegmentAtIndex: 16removeAllSegments 16removeSegmentAtIndex:animated: 16setBackgroundImage:forState:barMetrics: 17setContentOffset:forSegmentAtIndex: 18setContentPositionAdjustment:forSegmentType:barMetrics: 18setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics: 19setEnabled:forSegmentAtIndex: 20setImage:forSegmentAtIndex: 20setTitle:forSegmentAtIndex: 21

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

2

Page 3: UISegmentedControl Class

setTitleTextAttributes:forState: 22setWidth:forSegmentAtIndex: 22titleForSegmentAtIndex: 23titleTextAttributesForState: 23widthForSegmentAtIndex: 24

Constants 25UISegmentedControlStyle 25Segment Selection 26UISegmentedControlSegment 26

Deprecated UISegmentedControl Methods 28Deprecated in iOS 7.0 28

segmentedControlStyle 28

Document Revision History 29

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

3

Contents

Page 4: UISegmentedControl Class

Inherits from UIControl : UIView : UIResponder : NSObject

Conforms to NSCoding

NSCoding (UIView)

UIAppearance (UIView)

UIAppearanceContainer (UIView)

UIDynamicItem (UIView)

NSObject (NSObject)

Framework /System/Library/Frameworks/UIKit.framework

Availability Available in iOS 2.0 and later.

Companion guide “Segmented Controls”

Declared in UISegmentedControl.h

Related sample code iPhoneACFileConvertTest

iPhoneExtAudioFileConvertTest

NavBar

Quartz2D for iOS

UICatalog

OverviewA UISegmentedControl object is a horizontal control made of multiple segments, each segment functioningas 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). TheUISegmentedControl object automatically resizes segments to fit proportionally within their superviewunless they have a specific width set. When you add and remove segments, you can request that the actionbe animated with sliding and fading effects.

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

4

UISegmentedControl Class Reference

Page 5: UISegmentedControl Class

You register the target-action methods for a segmented control using the UIControlEventValueChangedconstant 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 (bluebackground) when the user touches it. The disclosure button is always momentary and doesn’t affect theactual selection.

● In versions of iOS prior to 3.0, if a segmented control has only two segments, then it behaves like aswitch—tapping the currently-selected segment causes the other segment to be selected. On iOS 3.0 andlater, tapping the currently-selected segment does not cause the other segment to be selected.

Customizing AppearanceIn 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 theappearance 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 beused by other states which don’t have a custom value set. Similarly, when a property is dependent on the barmetrics (on the iPhone in landscape orientation, bars have a different height from standard), you should makesure you specify a value for UIBarMetricsDefault.

In the case of the segmented control, appearance properties for UIBarMetricsLandscapePhone are onlyrespected for segmented controls in the smaller navigation and toolbars that are used in landscape orientationon the iPhone.

To provide complete customization, you need to provide divider images for different state combinations, usingsetDividerImage: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];

UISegmentedControl Class ReferenceOverview

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

5

Page 6: UISegmentedControl Class

// Image between segment selected on the right and unselected on the right.

[mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal

rightSegmentState:UIControlStateSelected barMetrics:barMetrics];

For more information about appearance and behavior configuration, see “Segmented Controls”.

Tasks

Initializing a Segmented Control

– initWithItems: (page 14)Initializes and returns a segmented control with segments having the given titles or images.

Managing Segment Content

– setImage:forSegmentAtIndex: (page 20)Sets the content of a segment to a given image.

– imageForSegmentAtIndex: (page 13)Returns the image for a specific segment

– setTitle:forSegmentAtIndex: (page 21)Sets the title of a segment.

– titleForSegmentAtIndex: (page 23)Returns the title of the specified segment.

Managing Segments

– insertSegmentWithImage:atIndex:animated: (page 14)Inserts a segment at a specified position in the receiver and gives it an image as content.

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

numberOfSegments (page 9) propertyReturns the number of segments the receiver has. (read-only)

UISegmentedControl Class ReferenceTasks

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

6

Page 7: UISegmentedControl Class

– removeAllSegments (page 16)Removes all segments of the receiver

– removeSegmentAtIndex:animated: (page 16)Removes the specified segment from the receiver, optionally animating the transition.

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

Managing Segment Behavior and Appearance

momentary (page 9) propertyA Boolean value that determines whether segments in the receiver show selected state.

– setEnabled:forSegmentAtIndex: (page 20)Enables the specified segment.

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

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

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

– setWidth:forSegmentAtIndex: (page 22)Sets the width of the specified segment of the receiver.

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

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

segmentedControlStyle (page 28) property Deprecated in iOS 7.0The style of the segmented control. (Deprecated. Segmented controls no longer support multiple stylesusing this functionality. Use the other appearance modification methods and properties to customize asegmented control.)

Customizing Appearance

tintColor (page 10) propertyThe tint color to apply to key elements in the segmented control.

UISegmentedControl Class ReferenceTasks

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

7

Page 8: UISegmentedControl Class

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

– setBackgroundImage:forState:barMetrics: (page 17)Sets the background image for a given state and bar metrics.

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

– setContentPositionAdjustment:forSegmentType:barMetrics: (page 18)Returns the content positioning offset for a given segment and bar metrics.

– dividerImageForLeftSegmentState:rightSegmentState:barMetrics: (page 12)Returns the divider image used for a given combination of left and right segment states and bar metrics.

– setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics: (page 19)Sets the divider image used for a given combination of left and right segment states and bar metrics.

– titleTextAttributesForState: (page 23)Returns the text attributes of the title for a given control state.

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

Properties

apportionsSegmentWidthsByContent

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

@property(nonatomic) BOOL apportionsSegmentWidthsByContent

DiscussionIf the value of this property is YES, for segments whose width value is 0, the control attempts to adjust segmentwidths based on their content widths.

The default is NO.

AvailabilityAvailable in iOS 5.0 and later.

Declared inUISegmentedControl.h

UISegmentedControl Class ReferenceProperties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

8

Page 9: UISegmentedControl Class

momentary

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

@property(nonatomic, getter=isMomentary) BOOL momentary

DiscussionThe default value of this property is NO. If it is set to YES, segments in the control do not show selected stateand do not update the value of selectedSegmentIndex (page 9) after tracking ends.

AvailabilityAvailable in iOS 2.0 and later.

Related Sample CodeNavBar

Declared inUISegmentedControl.h

numberOfSegments

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

@property(nonatomic, readonly) NSUInteger numberOfSegments

AvailabilityAvailable in iOS 2.0 and later.

Declared inUISegmentedControl.h

selectedSegmentIndex

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

@property(nonatomic) NSInteger selectedSegmentIndex

DiscussionThe default value is UISegmentedControlNoSegment (page 26) (no segment selected) until the user touchesa segment. Set this property to -1 to turn off the current selection. UISegmentedControl ignores this propertywhen the control is in momentary mode. When the user touches a segment to change the selection, the controlevent UIControlEventValueChanged is generated; if the segmented control is set up to respond to thiscontrol event, it sends a action message to its target.

UISegmentedControl Class ReferenceProperties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

9

Page 10: UISegmentedControl Class

AvailabilityAvailable in iOS 2.0 and later.

See Also @property momentary (page 9)

Related Sample CodeiPhoneCoreDataRecipesNavBarQuartz2D for iOSState Restoration of Child View ControllersUICatalog

Declared inUISegmentedControl.h

tintColor

The tint color to apply to key elements in the segmented control.

@property(nonatomic, retain) UIColor *tintColor

DiscussionIn iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussionof tintColor at the UIView level for more information.

AvailabilityAvailable in iOS 2.0 and later.

Related Sample CodeGLPaintNavBarPVRTextureLoaderUICatalog

Declared inUISegmentedControl.h

UISegmentedControl Class ReferenceProperties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

10

Page 11: UISegmentedControl Class

Instance Methods

backgroundImageForState:barMetrics:

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

- (UIImage *)backgroundImageForState:(UIControlState)statebarMetrics:(UIBarMetrics)barMetrics

Parametersstate

A control state.

barMetricsBar metrics.

Return ValueThe background image for state and barMetrics.

DiscussionFor more details, see setBackgroundImage:forState:barMetrics: (page 17).

AvailabilityAvailable in iOS 5.0 and later.

Declared inUISegmentedControl.h

contentOffsetForSegmentAtIndex:

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

- (CGSize)contentOffsetForSegmentAtIndex:(NSUInteger)segment

Parameterssegment

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

Return ValueThe offset (specified as a CGSize structure) from the origin of the segment at which to draw the segment’scontent.

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

11

Page 12: UISegmentedControl Class

AvailabilityAvailable in iOS 2.0 and later.

See Also– setContentOffset:forSegmentAtIndex: (page 18)

Declared inUISegmentedControl.h

contentPositionAdjustmentForSegmentType:barMetrics:

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

-(UIOffset)contentPositionAdjustmentForSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlonebarMetrics:(UIBarMetrics)barMetrics

ParametersleftCenterRightOrAlone

An identifier for a segment.

barMetricsBar metrics.

Return ValueThe content positioning offset for the segment identified by leftCenterRightOrAlone and barMetrics.

DiscussionFor more details, see setContentPositionAdjustment:forSegmentType:barMetrics: (page 18).

AvailabilityAvailable in iOS 5.0 and later.

See Also– setContentPositionAdjustment:forSegmentType:barMetrics: (page 18)

Declared inUISegmentedControl.h

dividerImageForLeftSegmentState:rightSegmentState:barMetrics:

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

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

12

Page 13: UISegmentedControl Class

- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)leftStaterightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

ParametersleftState

The state of the left segment.

rightStateThe state of the right segment.

barMetricsBar metrics.

Return ValueThe divider image used for the given combination of left and right segment states and bar metrics

AvailabilityAvailable in iOS 5.0 and later.

Declared inUISegmentedControl.h

imageForSegmentAtIndex:

Returns the image for a specific segment

- (UIImage *)imageForSegmentAtIndex:(NSUInteger)segment.

Parameterssegment

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

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

AvailabilityAvailable in iOS 2.0 and later.

See Also– setImage:forSegmentAtIndex: (page 20)

Related Sample CodeUICatalog

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

13

Page 14: UISegmentedControl Class

Declared inUISegmentedControl.h

initWithItems:

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

- (id)initWithItems:(NSArray *)items

Parametersitems

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

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

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

AvailabilityAvailable in iOS 2.0 and later.

Declared inUISegmentedControl.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)segmentanimated:(BOOL)animated

Parametersimage

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

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

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

14

Page 15: UISegmentedControl Class

animatedYES if the insertion of the new segment should be animated, otherwise NO.

AvailabilityAvailable in iOS 2.0 and later.

See Also– insertSegmentWithTitle:atIndex:animated: (page 15)– removeSegmentAtIndex:animated: (page 16)

Declared inUISegmentedControl.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)segmentanimated:(BOOL)animated

Parameterstitle

A string to use as the segment’s title.

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

animatedYES if the insertion of the new segment should be animated, otherwise NO.

AvailabilityAvailable in iOS 2.0 and later.

See Also– insertSegmentWithImage:atIndex:animated: (page 14)– removeSegmentAtIndex:animated: (page 16)

Declared inUISegmentedControl.h

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

15

Page 16: UISegmentedControl Class

isEnabledForSegmentAtIndex:

Returns whether the indicated segment is enabled.

- (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment

Parameterssegment

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

Return ValueYES if the given segment is enabled and NO if the segment is disabled. By default, segments are enabled.

AvailabilityAvailable in iOS 2.0 and later.

See Also– setEnabled:forSegmentAtIndex: (page 20)

Declared inUISegmentedControl.h

removeAllSegments

Removes all segments of the receiver

- (void)removeAllSegments

AvailabilityAvailable in iOS 2.0 and later.

See Also– removeSegmentAtIndex:animated: (page 16)

Declared inUISegmentedControl.h

removeSegmentAtIndex:animated:

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

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

16

Page 17: UISegmentedControl Class

- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated

Parameterssegment

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

animatedYES if the removal of the new segment should be animated, otherwise NO.

AvailabilityAvailable in iOS 2.0 and later.

See Also– removeAllSegments (page 16)– insertSegmentWithImage:atIndex:animated: (page 14)– insertSegmentWithTitle:atIndex:animated: (page 15)

Declared inUISegmentedControl.h

setBackgroundImage:forState:barMetrics:

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

- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)statebarMetrics:(UIBarMetrics)barMetrics

ParametersbackgroundImage

The background image to use for state and barMetrics.

stateA control state.

barMetricsBar metrics.

DiscussionIf backgroundImage is an image returned from resizableImageWithCapInsets:, the cap widths arecalculated from that information.

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

17

Page 18: UISegmentedControl Class

If backgroundImage is not an image returned from resizableImageWithCapInsets:, the cap width iscalculated by subtracting one from the image's width then dividing by 2. The cap widths are used as themargins for text placement. To adjust the margin, use the margin adjustment methods.

For more details, see backgroundImageForState:barMetrics: (page 11).

AvailabilityAvailable in iOS 5.0 and later.

Related Sample CodeUICatalog

Declared inUISegmentedControl.h

setContentOffset:forSegmentAtIndex:

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

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

Parametersoffset

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

segmentAn index number identifying a segment in the control. It must be a number between 0 and the numberof segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned toit.

AvailabilityAvailable in iOS 2.0 and later.

See Also– contentOffsetForSegmentAtIndex: (page 11)

Declared inUISegmentedControl.h

setContentPositionAdjustment:forSegmentType:barMetrics:

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

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

18

Page 19: UISegmentedControl Class

- (void)setContentPositionAdjustment:(UIOffset)adjustmentforSegmentType:(UISegmentedControlSegment)leftCenterRightOrAlonebarMetrics:(UIBarMetrics)barMetrics

Parametersadjustment

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

leftCenterRightOrAloneAn identifier for a segment type.

barMetricsBar metrics.

DiscussionYou use this method to adjust the position of a title or image within the given segment of a segmented control.

AvailabilityAvailable in iOS 5.0 and later.

See Also– contentPositionAdjustmentForSegmentType:barMetrics: (page 12)

Declared inUISegmentedControl.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 *)dividerImageforLeftSegmentState:(UIControlState)leftStaterightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics

ParametersdividerImage

The divider image to use.

leftStateThe state of the left segment.

rightStateThe state of the right segment.

barMetricsBar metrics.

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

19

Page 20: UISegmentedControl Class

AvailabilityAvailable in iOS 5.0 and later.

Related Sample CodeUICatalog

Declared inUISegmentedControl.h

setEnabled:forSegmentAtIndex:

Enables the specified segment.

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

Parametersenabled

YES to enable the specified segment or NO to disable the segment. By default, segments are enabled.

segmentAn index number identifying a segment in the control. It must be a number between 0 and the numberof segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned toit.

AvailabilityAvailable in iOS 2.0 and later.

See Also– isEnabledForSegmentAtIndex: (page 16)

Declared inUISegmentedControl.h

setImage:forSegmentAtIndex:

Sets the content of a segment to a given image.

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

Parametersimage

An image object to display in the segment. .

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

20

Page 21: UISegmentedControl Class

segmentAn index number identifying a segment in the control. It must be a number between 0 and the numberof segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned toit.

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

AvailabilityAvailable in iOS 2.0 and later.

See Also– imageForSegmentAtIndex: (page 13)

Declared inUISegmentedControl.h

setTitle:forSegmentAtIndex:

Sets the title of a segment.

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

Parameterstitle

A string to display in the segment as its title.

segmentAn index number identifying a segment in the control. It must be a number between 0 and the numberof segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned toit.

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

AvailabilityAvailable in iOS 2.0 and later.

See Also– titleForSegmentAtIndex: (page 23)

Declared inUISegmentedControl.h

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

21

Page 22: UISegmentedControl Class

setTitleTextAttributes:forState:

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

- (void)setTitleTextAttributes:(NSDictionary *)attributesforState:(UIControlState)state

Parametersattributes

The text attributes of the title for state.

stateA control state.

DiscussionThe attributes dictionary can specify the font, text color, text shadow color, and text shadow offset for the titlein the text attributes dictionary, using the keys in NSString UIKit Additions Reference .

AvailabilityAvailable in iOS 5.0 and later.

Related Sample CodeUICatalog

Declared inUISegmentedControl.h

setWidth:forSegmentAtIndex:

Sets the width of the specified segment of the receiver.

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

Parameterswidth

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

segmentAn index number identifying a segment in the control. It must be a number between 0 and the numberof segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned toit.

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

22

Page 23: UISegmentedControl Class

AvailabilityAvailable in iOS 2.0 and later.

See Also– widthForSegmentAtIndex: (page 24)

Declared inUISegmentedControl.h

titleForSegmentAtIndex:

Returns the title of the specified segment.

- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment

Parameterssegment

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

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

AvailabilityAvailable in iOS 2.0 and later.

See Also– setTitle:forSegmentAtIndex: (page 21)

Declared inUISegmentedControl.h

titleTextAttributesForState:

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

- (NSDictionary *)titleTextAttributesForState:(UIControlState)state

Parametersstate

A control state.

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

23

Page 24: UISegmentedControl Class

Return ValueThe text attributes of the title for state.

DiscussionFor more details, see setTitleTextAttributes:forState: (page 22)

AvailabilityAvailable in iOS 5.0 and later.

Declared inUISegmentedControl.h

widthForSegmentAtIndex:

Returns the width of the indicated segment of the receiver.

- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment

Parameterssegment

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

Return ValueA float value specifying the width of the segment. If the value is {0.0}, UISegmentedControl automaticallysizes the segment.

AvailabilityAvailable in iOS 2.0 and later.

See Also– setWidth:forSegmentAtIndex: (page 22)

Declared inUISegmentedControl.h

UISegmentedControl Class ReferenceInstance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

24

Page 25: UISegmentedControl Class

Constants

UISegmentedControlStyle

The styles of the segmented control. (Deprecated. Use tintColor (page 10) instead.)

typedef enum {UISegmentedControlStylePlain,UISegmentedControlStyleBordered,UISegmentedControlStyleBar,UISegmentedControlStyleBezeled,

} UISegmentedControlStyle;

ConstantsUISegmentedControlStylePlain

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

Available in iOS 2.0 and later. Deprecated in iOS 7.0.

Declared in UISegmentedControl.h.

UISegmentedControlStyleBorderedThe large bordered style for segmented controls.

Available in iOS 2.0 and later. Deprecated in iOS 7.0.

Declared in UISegmentedControl.h.

UISegmentedControlStyleBarThe small toolbar style for segmented controls. Segmented controls in this style can have a tint color (seetintColor (page 10)).

Available in iOS 2.0 and later. Deprecated in iOS 7.0.

Declared in UISegmentedControl.h.

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

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

Available in iOS 4.0 and later. Deprecated in iOS 5.1.

Declared in UISegmentedControl.h.

DiscussionYou use these constants as values for the segmentedControlStyle (page 28) property.

AvailabilityAvailable in iOS 2.0 and later. Deprecated in iOS 7.0.

UISegmentedControl Class ReferenceConstants

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

25

Page 26: UISegmentedControl Class

Segment Selection

A constant for indicating that no segment is selected.

enum {UISegmentedControlNoSegment = -1

};

ConstantsUISegmentedControlNoSegment

A segment index value indicating that there is no selected segment. See selectedSegmentIndex (page9) 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;

ConstantsUISegmentedControlSegmentAny

Specifies any segment.

Available in iOS 5.0 and later.

Declared in UISegmentedControl.h.

UISegmentedControlSegmentLeftThe capped, leftmost segment.

Only applies when numSegments > 1.

Available in iOS 5.0 and later.

Declared in UISegmentedControl.h.

UISegmentedControl Class ReferenceConstants

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

26

Page 27: UISegmentedControl Class

UISegmentedControlSegmentCenterAny segment between the left and rightmost segments.

Only applies when numSegments > 2.

Available in iOS 5.0 and later.

Declared in UISegmentedControl.h.

UISegmentedControlSegmentRightThe capped,rightmost segment. .

Only applies when numSegments > 1.

Available in iOS 5.0 and later.

Declared in UISegmentedControl.h.

UISegmentedControlSegmentAloneThe standalone segment, capped on both ends.

Only applies when numSegments = 1.

Available in iOS 5.0 and later.

Declared in UISegmentedControl.h.

UISegmentedControl Class ReferenceConstants

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

27

Page 28: UISegmentedControl Class

A method identified as deprecated has been superseded and may become unsupported in the future.

Deprecated in iOS 7.0

segmentedControlStyle

The style of the segmented control. (Deprecated in iOS 7.0. Segmented controls no longer support multiple stylesusing this functionality. Use the other appearance modification methods and properties to customize a segmentedcontrol.)

@property(nonatomic) UISegmentedControlStyle segmentedControlStyle

DiscussionThe default style is UISegmentedControlStylePlain (page 25). See “UISegmentedControlStyle” (page 25)for descriptions of valid constants.

AvailabilityAvailable in iOS 2.0 and later.

Deprecated in iOS 7.0.

Related Sample CodeGLPaintNavBarUICatalog

Declared inUISegmentedControl.h

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

28

Deprecated UISegmentedControl Methods

Page 29: UISegmentedControl Class

This table describes the changes to UISegmentedControl Class Reference .

NotesDate

Updated deprecation information for iOS 7.2013-09-18

Updated for iOS v5.0.2011-10-12

Added the UISegmentedControlStyleBezeled constant.2010-05-17

Fixed incorrect see also links.2010-03-23

Updated for iOS 3.0.2009-03-06

Corrected description of selectedSegmentIndex property.2008-09-30

New document that describes the class implementing horizontal controlsmade of multiple segments, each segment functioning as a discrete button.

2008-06-10

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

29

Document Revision History

Page 30: UISegmentedControl Class

Apple Inc.Copyright © 2013 Apple Inc.All rights reserved.

No part of this publication may be reproduced,stored in a retrieval system, or transmitted, in anyform or by any means, mechanical, electronic,photocopying, recording, or otherwise, withoutprior written permission of Apple Inc., with thefollowing exceptions: Any person is herebyauthorized to store documentation on a singlecomputer for personal use only and to printcopies of documentation for personal useprovided that the documentation containsApple’s copyright notice.

No licenses, express or implied, are granted withrespect to any of the technology described in thisdocument. Apple retains all intellectual propertyrights associated with the technology describedin this document. This document is intended toassist application developers to developapplications only for Apple-labeled computers.

Apple Inc.1 Infinite LoopCupertino, CA 95014408-996-1010

Apple, the Apple logo, iPhone, and Quartz aretrademarks of Apple Inc., registered in the U.S.and other countries.

iOS is a trademark or registered trademark ofCisco in the U.S. and other countries and is usedunder license.

Even though Apple has reviewed this document,APPLE MAKES NO WARRANTY OR REPRESENTATION,EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THISDOCUMENT, ITS QUALITY, ACCURACY,MERCHANTABILITY, OR FITNESS FOR A PARTICULARPURPOSE.ASARESULT, THISDOCUMENT ISPROVIDED“AS IS,” AND YOU, THE READER, ARE ASSUMING THEENTIRE RISK AS TO ITS QUALITY AND ACCURACY.

IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT,INDIRECT, SPECIAL, INCIDENTAL, ORCONSEQUENTIALDAMAGES RESULTING FROM ANY DEFECT ORINACCURACY IN THIS DOCUMENT, even if advised ofthe possibility of such damages.

THE WARRANTY AND REMEDIES SET FORTH ABOVEARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORALOR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer,agent, or employee is authorized to make anymodification, extension, or addition to this warranty.

Some states do not allow the exclusion or limitationof implied warranties or liability for incidental orconsequential damages, so the above limitation orexclusion may not apply to you. This warranty givesyou specific legal rights, and youmay also have otherrights which vary from state to state.