Sie sind auf Seite 1von 30

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 9 numberOfSegments 9 selectedSegmentIndex 9 tintColor 10 Instance Methods 11 backgroundImageForState:barMetrics: 11 contentOffsetForSegmentAtIndex: 11 contentPositionAdjustmentForSegmentType:barMetrics: 12 dividerImageForLeftSegmentState:rightSegmentState:barMetrics: 12 imageForSegmentAtIndex: 13 initWithItems: 14 insertSegmentWithImage:atIndex:animated: 14 insertSegmentWithTitle:atIndex:animated: 15 isEnabledForSegmentAtIndex: 16 removeAllSegments 16 removeSegmentAtIndex:animated: 16 setBackgroundImage:forState:barMetrics: 17 setContentOffset:forSegmentAtIndex: 18 setContentPositionAdjustment:forSegmentType:barMetrics: 18 setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics: 19 setEnabled:forSegmentAtIndex: 20 setImage:forSegmentAtIndex: 20 setTitle:forSegmentAtIndex: 21

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

Contents

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

Deprecated UISegmentedControl Methods 28


Deprecated in iOS 7.0 28 segmentedControlStyle 28

Document Revision History 29

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

UISegmentedControl Class Reference

Inherits from Conforms to

UIControl : UIView : UIResponder : NSObject NSCoding NSCoding (UIView) UIAppearance (UIView) UIAppearanceContainer (UIView) UIDynamicItem (UIView) NSObject (NSObject)

Framework Availability Companion guide Declared in Related sample code

/System/Library/Frameworks/UIKit.framework Available in iOS 2.0 and later. Segmented Controls UISegmentedControl.h

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.

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

UISegmentedControl Class Reference Overview

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

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

UISegmentedControl Class Reference Tasks

// 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. (page 13) Returns the image for a specific segment (page 21) Sets the title of a segment.

imageForSegmentAtIndex:

setTitle:forSegmentAtIndex:

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. (page 15) Inserts a segment at a specific position in the receiver and gives it a title as content. (page 9) property Returns the number of segments the receiver has. (read-only)

insertSegmentWithTitle:atIndex:animated:

numberOfSegments

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

UISegmentedControl Class Reference Tasks

removeAllSegments

(page 16) Removes all segments of the receiver (page 16) Removes the specified segment from the receiver, optionally animating the transition. (page 9) property The index number identifying the selected segment (that is, the last segment touched).

removeSegmentAtIndex:animated:

selectedSegmentIndex

Managing Segment Behavior and Appearance


momentary

(page 9) property A Boolean value that determines whether segments in the receiver show selected state. (page 20) Enables the specified segment.

setEnabled:forSegmentAtIndex:

isEnabledForSegmentAtIndex:

(page 16) Returns whether the indicated segment is enabled. (page 18) Adjusts the offset for drawing the content (image or text) of the specified segment. (page 11) Returns the offset for drawing the content (image or text) of the specified segment. (page 22) Sets the width of the specified segment of the receiver. (page 24) Returns the width of the indicated segment of the receiver. (page 8) property Indicates whether the control attempts to adjust segment widths based on their content widths. (page 28) property Deprecated in iOS 7.0 The style of the segmented control. (Deprecated. Segmented controls no longer support multiple styles using this functionality. Use the other appearance modification methods and properties to customize a segmented control.)

setContentOffset:forSegmentAtIndex:

contentOffsetForSegmentAtIndex:

setWidth:forSegmentAtIndex:

widthForSegmentAtIndex:

apportionsSegmentWidthsByContent

segmentedControlStyle

Customizing Appearance
tintColor

(page 10) property The tint color to apply to key elements in the segmented control.

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

UISegmentedControl Class Reference Properties

backgroundImageForState:barMetrics:

(page 11) Returns the background image for a given state and bar metrics. (page 17) Sets the background image for a given state and bar metrics. (page 12) Returns the positioning offset for a given segment and bar metrics. (page 18) Returns the content positioning offset for a given segment and bar metrics. (page 12) Returns the divider image used for a given combination of left and right segment states and bar metrics. (page 19) Sets the divider image used for a given combination of left and right segment states and bar metrics. (page 23) Returns the text attributes of the title for a given control state. (page 22) Sets the text attributes of the title for a given control state.

setBackgroundImage:forState:barMetrics:

contentPositionAdjustmentForSegmentType:barMetrics:

setContentPositionAdjustment:forSegmentType:barMetrics:

dividerImageForLeftSegmentState:rightSegmentState:barMetrics:

setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:

titleTextAttributesForState:

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

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

UISegmentedControl Class Reference Properties

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

selectedSegmentIndex
The index number identifying the selected segment (that is, the last segment touched).
@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.

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

UISegmentedControl Class Reference Properties

Availability Available in iOS 2.0 and later. See Also


@property momentary

(page 9)

Related Sample Code

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

tintColor
The tint color to apply to key elements in the segmented control.
@property(nonatomic, retain) UIColor *tintColor

Discussion In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information. Availability Available in iOS 2.0 and later.
Related Sample Code

GLPaint NavBar PVRTextureLoader UICatalog Declared in

UISegmentedControl.h

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

10

UISegmentedControl Class Reference Instance Methods

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. Return Value The offset (specified as a CGSize structure) from the origin of the segment at which to draw the segments content.

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

11

UISegmentedControl Class Reference Instance Methods

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 18). Availability Available in iOS 5.0 and later. See Also
setContentPositionAdjustment:forSegmentType:barMetrics:

(page 18)

Declared in
UISegmentedControl.h

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

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

12

UISegmentedControl Class Reference Instance Methods

- (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. See Also
setImage:forSegmentAtIndex:

(page 20)

Related Sample Code

UICatalog

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

13

UISegmentedControl Class Reference Instance Methods

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.


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.

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

14

UISegmentedControl Class Reference Instance Methods

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 16)

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. See Also


insertSegmentWithImage:atIndex:animated: removeSegmentAtIndex:animated:

(page 14)

(page 16)

Declared in
UISegmentedControl.h

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

15

UISegmentedControl Class Reference Instance Methods

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 16)

Declared in
UISegmentedControl.h

removeSegmentAtIndex:animated:
Removes the specified segment from the receiver, optionally animating the transition.

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

16

UISegmentedControl Class Reference Instance Methods

- (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)

(page 14) insertSegmentWithTitle:atIndex:animated: (page 15)


insertSegmentWithImage:atIndex:animated:

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

Bar metrics. Discussion If backgroundImage is an image returned from resizableImageWithCapInsets:, the cap widths are calculated from that information.

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

17

UISegmentedControl Class Reference Instance Methods

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. 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. See Also
contentOffsetForSegmentAtIndex:

(page 11)

Declared in
UISegmentedControl.h

setContentPositionAdjustment:forSegmentType:barMetrics:
Returns the content positioning offset for a given segment and bar metrics.

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

18

UISegmentedControl Class Reference Instance Methods

- (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

Parameters
dividerImage

The divider image to use.


leftState

The state of the left segment.


rightState

The state of the right segment.


barMetrics

Bar metrics.

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

19

UISegmentedControl Class Reference Instance Methods

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

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

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

20

UISegmentedControl Class Reference Instance Methods

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. Availability Available in iOS 2.0 and later. See Also
titleForSegmentAtIndex:

(page 23)

Declared in
UISegmentedControl.h

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

21

UISegmentedControl Class Reference Instance Methods

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

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.

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

22

UISegmentedControl Class Reference Instance Methods

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

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

Parameters
state

A control state.

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

23

UISegmentedControl Class Reference Instance Methods

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)

Declared in
UISegmentedControl.h

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

24

UISegmentedControl Class Reference Constants

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

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. Deprecated in iOS 7.0. Declared in UISegmentedControl.h.
UISegmentedControlStyleBordered

The large bordered style for segmented controls. Available in iOS 2.0 and later. Deprecated in iOS 7.0. 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. Deprecated in iOS 7.0. 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)). 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. Discussion You use these constants as values for the segmentedControlStyle (page 28) property. Availability
Available in iOS 2.0 and later. Deprecated in iOS 7.0.

2013-09-18 | Copyright 2013 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.

2013-09-18 | Copyright 2013 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.

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

27

Deprecated UISegmentedControl Methods

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 styles using this functionality. Use the other appearance modification methods and properties to customize a 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. Deprecated in iOS 7.0.
Related Sample Code

GLPaint NavBar UICatalog

Declared in
UISegmentedControl.h

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

28

Document Revision History

This table describes the changes to UISegmentedControl Class Reference .

Date 2013-09-18 2011-10-12 2010-05-17 2010-03-23 2009-03-06 2008-09-30 2008-06-10

Notes Updated deprecation information for iOS 7. Updated for iOS v5.0. Added the UISegmentedControlStyleBezeled constant. Fixed incorrect see also links. Updated for iOS 3.0. Corrected description of selectedSegmentIndex property. New document that describes the class implementing horizontal controls made of multiple segments, each segment functioning as a discrete button.

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

29

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

Das könnte Ihnen auch gefallen