UISegmentedControl Class

download UISegmentedControl Class

of 29

description

Ios Development control

Transcript of UISegmentedControl Class

  • UISegmentedControl Class Reference

  • Contents

    UISegmentedControl Class Reference 4Overview 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.

    2

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

    3

    Contents

  • 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

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

    4

    UISegmentedControl Class Reference

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

    UISegmentedControl Class ReferenceOverview

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

    5

  • 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 21)

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

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

    removeAllSegments (page 16)

    Removes all segments of the receiver

    removeSegmentAtIndex:animated: (page 17)

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

    selectedSegmentIndex (page 9) property

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

    UISegmentedControl Class ReferenceTasks

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

    6

  • Managing Segment Behavior and Appearance

    momentary (page 8) property

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

    segmentedControlStyle (page 9) property

    The style of the segmented control.

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

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

    Customizing Appearance

    tintColor (page 10) property

    The tint color of the segmented control.

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

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

    dividerImageForLeftSegmentState:rightSegmentState:barMetrics: (page 13)

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

    UISegmentedControl Class ReferenceTasks

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

    7

  • 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 24)

    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 segment

    widths based on their content widths.

    The default is NO.

    AvailabilityAvailable in iOS 5.0 and later.

    Declared inUISegmentedControl.h

    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 state

    and do not update the value of selectedSegmentIndex (page 9) after tracking ends.

    AvailabilityAvailable in iOS 2.0 and later.

    UISegmentedControl Class ReferenceProperties

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

    8

  • 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

    segmentedControlStyle

    The style of the segmented control.

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

    Related Sample CodeGLPaintNavBarUICatalog

    Declared inUISegmentedControl.h

    selectedSegmentIndex

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

    UISegmentedControl Class ReferenceProperties

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

    9

  • @property(nonatomic) NSInteger selectedSegmentIndex

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

    AvailabilityAvailable in iOS 2.0 and later.

    See Also @property momentary (page 8)

    Related Sample CodeiPhoneCoreDataRecipesNavBarQuartz2D for iOSState Restoration of Child View ControllersUICatalog

    Declared inUISegmentedControl.h

    tintColor

    The tint color of the segmented control.

    @property(nonatomic, retain) UIColor *tintColor

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

    AvailabilityAvailable in iOS 2.0 and later.

    Related Sample CodeGLPaintNavBarPVRTextureLoaderUICatalog

    UISegmentedControl Class ReferenceProperties

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

    10

  • Declared inUISegmentedControl.h

    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.

    barMetrics

    Bar 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 number

    of segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned to

    it.

    UISegmentedControl Class ReferenceInstance Methods

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

    11

  • Return ValueThe offset (specified as a CGSize structure) from the origin of the segment at which to draw the segments

    content.

    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.

    barMetrics

    Bar metrics.

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

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

    AvailabilityAvailable in iOS 5.0 and later.

    See Also setContentPositionAdjustment:forSegmentType:barMetrics: (page 19)

    Declared inUISegmentedControl.h

    UISegmentedControl Class ReferenceInstance Methods

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

    12

  • dividerImageForLeftSegmentState:rightSegmentState:barMetrics:

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

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

    ParametersleftState

    The state of the left segment.

    rightState

    The state of the right segment.

    barMetrics

    Bar 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 number

    of segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned to

    it.

    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.

    UISegmentedControl Class ReferenceInstance Methods

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

    13

  • See Also setImage:forSegmentAtIndex: (page 21)

    Related Sample CodeUICatalog

    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.

    UISegmentedControl Class ReferenceInstance Methods

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

    14

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

    AvailabilityAvailable in iOS 2.0 and later.

    See Also insertSegmentWithTitle:atIndex:animated: (page 15) removeSegmentAtIndex:animated: (page 17)

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

    AvailabilityAvailable in iOS 2.0 and later.

    UISegmentedControl Class ReferenceInstance Methods

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

    15

  • See Also insertSegmentWithImage:atIndex:animated: (page 14) removeSegmentAtIndex:animated: (page 17)

    Declared inUISegmentedControl.h

    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 number

    of segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned to

    it.

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

    UISegmentedControl Class ReferenceInstance Methods

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

    16

  • Declared inUISegmentedControl.h

    removeSegmentAtIndex:animated:

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

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

    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.

    state

    A control state.

    UISegmentedControl Class ReferenceInstance Methods

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

    17

  • barMetrics

    Bar metrics.

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

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

    AvailabilityAvailable in iOS 2.0 and later.

    UISegmentedControl Class ReferenceInstance Methods

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

    18

  • See Also contentOffsetForSegmentAtIndex: (page 11)

    Declared inUISegmentedControl.h

    setContentPositionAdjustment:forSegmentType:barMetrics:

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

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

    Parametersadjustment

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

    leftCenterRightOrAlone

    An identifier for a segment type.

    barMetrics

    Bar 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

    UISegmentedControl Class ReferenceInstance Methods

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

    19

  • ParametersdividerImage

    The divider image to use.

    leftState

    The state of the left segment.

    rightState

    The state of the right segment.

    barMetrics

    Bar metrics.

    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.

    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.

    AvailabilityAvailable in iOS 2.0 and later.

    See Also isEnabledForSegmentAtIndex: (page 16)

    Declared inUISegmentedControl.h

    UISegmentedControl Class ReferenceInstance Methods

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

    20

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

    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.

    DiscussionA segment can only have an image or a title; it cant 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.

    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.

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

    UISegmentedControl Class ReferenceInstance Methods

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

    21

  • AvailabilityAvailable in iOS 2.0 and later.

    See Also titleForSegmentAtIndex: (page 23)

    Declared inUISegmentedControl.h

    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.

    state

    A control state.

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

    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

    UISegmentedControl Class ReferenceInstance Methods

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

    22

  • Parameterswidth

    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.

    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 number

    of segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned to

    it.

    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

    UISegmentedControl Class ReferenceInstance Methods

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

    23

  • titleTextAttributesForState:

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

    - (NSDictionary *)titleTextAttributesForState:(UIControlState)state

    Parametersstate

    A control state.

    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 number

    of segments (numberOfSegments (page 9)) minus 1; values exceeding this upper range are pinned to

    it.

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

    sizes the segment.

    AvailabilityAvailable in iOS 2.0 and later.

    See Also setWidth:forSegmentAtIndex: (page 22)

    UISegmentedControl Class ReferenceInstance Methods

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

    24

  • Declared inUISegmentedControl.h

    Constants

    UISegmentedControlStyle

    The styles of the segmented control.

    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.

    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.

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

    UISegmentedControl Class ReferenceConstants

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

    25

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

    ConstantsUISegmentedControlSegmentAny

    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.

    UISegmentedControl Class ReferenceConstants

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

    26

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

    UISegmentedControl Class ReferenceConstants

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

    27

  • This table describes the changes to UISegmentedControl Class Reference .

    NotesDate

    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 controls

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

    2008-06-10

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

    28

    Document Revision History

  • 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 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 containsApples 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 ISPROVIDEDAS 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.

    UISegmentedControl Class ReferenceContentsUISegmentedControl Class ReferenceOverviewCustomizing Appearance

    TasksInitializing a Segmented ControlManaging Segment ContentManaging SegmentsManaging Segment Behavior and AppearanceCustomizing Appearance

    PropertiesapportionsSegmentWidthsByContentmomentarynumberOfSegmentssegmentedControlStyleselectedSegmentIndextintColor

    Instance MethodsbackgroundImageForState:barMetrics:contentOffsetForSegmentAtIndex:contentPositionAdjustmentForSegmentType:barMetrics:dividerImageForLeftSegmentState:rightSegmentState:barMetrics:imageForSegmentAtIndex:initWithItems:insertSegmentWithImage:atIndex:animated:insertSegmentWithTitle:atIndex:animated:isEnabledForSegmentAtIndex:removeAllSegmentsremoveSegmentAtIndex:animated:setBackgroundImage:forState:barMetrics:setContentOffset:forSegmentAtIndex:setContentPositionAdjustment:forSegmentType:barMetrics:setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics:setEnabled:forSegmentAtIndex:setImage:forSegmentAtIndex:setTitle:forSegmentAtIndex:setTitleTextAttributes:forState:setWidth:forSegmentAtIndex:titleForSegmentAtIndex:titleTextAttributesForState:widthForSegmentAtIndex:

    ConstantsUISegmentedControlStyleSegment SelectionUISegmentedControlSegment

    Revision History