UIButton Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | UIButton.h |
Overview
An instance of the UIButton class implements a button on the touch screen. A button intercepts touch events and sends an action message to a target object when tapped. Methods for setting the target and action are inherited from UIControl. This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.
For information about basic view behaviors, see View Programming Guide for iOS.
Tasks
Creating Buttons
Configuring the Button Title
-
titleLabelproperty -
reversesTitleShadowWhenHighlightedproperty -
– setTitle:forState: -
– setAttributedTitle:forState: -
– setTitleColor:forState: -
– setTitleShadowColor:forState: -
– titleColorForState: -
– titleForState: -
– attributedTitleForState: -
– titleShadowColorForState:
Configuring Button Presentation
-
adjustsImageWhenHighlightedproperty -
adjustsImageWhenDisabledproperty -
showsTouchWhenHighlightedproperty -
– backgroundImageForState: -
– imageForState: -
– setBackgroundImage:forState: -
– setImage:forState: -
tintColorproperty
Configuring Edge Insets
-
contentEdgeInsetsproperty -
titleEdgeInsetsproperty -
imageEdgeInsetsproperty
Getting the Current State
-
buttonTypeproperty -
currentTitleproperty -
currentAttributedTitleproperty -
currentTitleColorproperty -
currentTitleShadowColorproperty -
currentImageproperty -
currentBackgroundImageproperty -
imageViewproperty
Getting Dimensions
-
– backgroundRectForBounds: -
– contentRectForBounds: -
– titleRectForContentRect: -
– imageRectForContentRect:
Deprecated Properties
-
fontproperty Deprecated in iOS 3.0 -
lineBreakModeproperty Deprecated in iOS 3.0 -
titleShadowOffsetproperty Deprecated in iOS 3.0
Properties
adjustsImageWhenDisabled
A Boolean value that determines whether the image changes when the button is disabled.
Discussion
If YES, the image is drawn darker when the button is disabled. The default value is YES.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hadjustsImageWhenHighlighted
A Boolean value that determines whether the image changes when the button is highlighted.
Discussion
If YES, the image is drawn lighter when the button is highlighted. The default value is YES.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hbuttonType
The button type. (read-only)
Discussion
See UIButtonType for the possible values.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hcontentEdgeInsets
The inset or outset margins for the rectangle surrounding all of the button’s content.
Discussion
Use this property to resize and reposition the effective drawing rectangle for the button content. The content comprises the button image and button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hcurrentAttributedTitle
The current styled title that is displayed on the button. (read-only)
Discussion
The value for this property reflects the title associated with the control’s current state. For states that do not have a custom title string associated with them, this method returns the attributed title that is currently displayed, which is typically the one associated with the UIControlStateNormal state.
Availability
- Available in iOS 6.0 and later.
Declared In
UIButton.hcurrentBackgroundImage
The current background image displayed on the button. (read-only)
Discussion
This value can be nil.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hcurrentImage
The current image displayed on the button. (read-only)
Discussion
This value can be nil.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hcurrentTitle
The current title that is displayed on the button. (read-only)
Discussion
The value for this property is set automatically whenever the button state changes. For states that do not have a custom title string associated with them, this method returns the title that is currently displayed, which is typically the one associated with the UIControlStateNormal state. The value may be nil.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hcurrentTitleColor
The color used to display the title. (read-only)
Discussion
This value is guaranteed not to be nil. The default value is white.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hcurrentTitleShadowColor
The color of the title’s shadow. (read-only)
Discussion
The default value is white.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.himageEdgeInsets
The inset or outset margins for the rectangle around the button’s image.
Discussion
Use this property to resize and reposition the effective drawing rectangle for the button image. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.himageView
The button’s image view. (read-only)
Discussion
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; |
button.imageView.exclusiveTouch = YES; |
The imageView property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.
Availability
- Available in iOS 3.0 and later.
Declared In
UIButton.hreversesTitleShadowWhenHighlighted
A Boolean value that determines whether the title shadow changes when the button is highlighted.
Discussion
If YES, the shadow changes from engrave to emboss appearance when highlighted. The default value is NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hshowsTouchWhenHighlighted
A Boolean value that determines whether tapping the button causes it to glow.
Discussion
If YES, the button glows when tapped; otherwise, it does not. The image and button behavior is not changed by the glow. The default value is NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.htintColor
The tint color for the button.
Discussion
The default value is nil.
This property is not valid for all button types.
Availability
- Available in iOS 5.0 and later.
Declared In
UIButton.htitleEdgeInsets
The inset or outset margins for the rectangle around the button’s title text.
Discussion
Use this property to resize and reposition the effective drawing rectangle for the button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
The insets you specify are applied to the title rectangle after that rectangle has been sized to fit the button’s text. Thus, positive inset values may actually clip the title text.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.htitleLabel
A view that displays the value of the currentTitle property for a button. (read-only)
Discussion
Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; |
button.titleLabel.font = [UIFont systemFontOfSize: 12]; |
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; |
Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.
The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.
Availability
- Available in iOS 3.0 and later.
See Also
Declared In
UIButton.hClass Methods
buttonWithType:
Creates and returns a new button of the specified type.
Parameters
- buttonType
The button type. See
UIButtonTypefor the possible values.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hInstance Methods
attributedTitleForState:
Returns the styled title associated with the specified state.
Parameters
- state
The state that uses the styled title. The possible values are described in
UIControlState.
Return Value
The title for the specified state. If no attributed title has been set for the specific state, this method returns the attributed title associated with the UIControlStateNormal state.
Availability
- Available in iOS 6.0 and later.
Declared In
UIButton.hbackgroundImageForState:
Returns the background image used for a button state.
Parameters
- state
The state that uses the background image. Possible values are described in
UIControlState.
Return Value
The background image used for the specified state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hbackgroundRectForBounds:
Returns the rectangle in which the receiver draws its background.
Parameters
- bounds
The bounding rectangle of the receiver.
Return Value
The bounds rectangle in which to draw any standard button content.
Discussion
The default implementation of this method returns the value in the bounds parameter. This rectangle represents the area in which the button draws its standard background content. Subclasses that provide custom background adornments can override this method and return a modified bounds rectangle to prevent the button from drawing over any custom content.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hcontentRectForBounds:
Returns the rectangle in which the receiver draws its entire content.
Parameters
- bounds
The bounding rectangle for the receiver.
Return Value
The rectangle in which the receiver draws its entire content.
Discussion
The content rectangle is the area needed to display the image and title including any padding and adjustments for alignment and other settings.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.himageForState:
Returns the image used for a button state.
Parameters
- state
The state that uses the image. Possible values are described in
UIControlState.
Return Value
The image used for the specified state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.himageRectForContentRect:
Returns the rectangle in which the receiver draws its image.
Parameters
- contentRect
The content rectangle for the receiver.
Return Value
The rectangle in which the receiver draws its image.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.hsetAttributedTitle:forState:
Sets the styled title to use for the specified state.
Parameters
- title
The styled text string so use for the title.
- state
The state that uses the specified title. The possible values are described in
UIControlState.
Discussion
Use this method to set the title of the button, including any relevant formatting information. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title.
At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.
Availability
- Available in iOS 6.0 and later.
Declared In
UIButton.hsetBackgroundImage:forState:
Sets the background image to use for the specified button state.
Parameters
- image
The background image to use for the specified state.
- state
The state that uses the specified image. The values are described in
UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hsetImage:forState:
Sets the image to use for the specified state.
Parameters
- image
The image to use for the specified state.
- state
The state that uses the specified title. The values are described in
UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hsetTitle:forState:
Sets the title to use for the specified state.
Parameters
- title
The title to use for the specified state.
- state
The state that uses the specified title. The possible values are described in
UIControlState.
Discussion
Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.
At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hsetTitleColor:forState:
Sets the color of the title to use for the specified state.
Parameters
- color
The color of the title to use for the specified state.
- state
The state that uses the specified color. The possible values are described in
UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hsetTitleShadowColor:forState:
Sets the color of the title shadow to use for the specified state.
Parameters
- color
The color of the title shadow to use for the specified state.
- state
The state that uses the specified color. The possible values are described in
UIControlState.
Discussion
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.htitleColorForState:
Returns the title color used for a state.
Parameters
- state
The state that uses the title color. The possible values are described in
UIControlState.
Return Value
The color of the title for the specified state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.htitleForState:
Returns the title associated with the specified state.
Parameters
- state
The state that uses the title. The possible values are described in
UIControlState.
Return Value
The title for the specified state. If no title has been set for the specific state, this method returns the title associated with the UIControlStateNormal state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.htitleRectForContentRect:
Returns the rectangle in which the receiver draws its title.
Parameters
- contentRect
The content rectangle for the receiver.
Return Value
The rectangle in which the receiver draws its title.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.htitleShadowColorForState:
Returns the shadow color of the title used for a state.
Parameters
- state
The state that uses the title shadow color. The possible values are described in
UIControlState.
Return Value
The color of the title’s shadow for the specified state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIButton.hConstants
UIButtonType
Specifies the style of a button.
typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
Constants
UIButtonTypeCustomNo button style.
Available in iOS 2.0 and later.
Declared in
UIButton.h.UIButtonTypeRoundedRectA rounded-rectangle style button.
Available in iOS 2.0 and later.
Declared in
UIButton.h.UIButtonTypeDetailDisclosureA detail disclosure button.
Available in iOS 2.0 and later.
Declared in
UIButton.h.UIButtonTypeInfoLightAn information button that has a light background.
Available in iOS 2.0 and later.
Declared in
UIButton.h.UIButtonTypeInfoDarkAn information button that has a dark background.
Available in iOS 2.0 and later.
Declared in
UIButton.h.UIButtonTypeContactAddA contact add button.
Available in iOS 2.0 and later.
Declared in
UIButton.h.
Availability
- Available in iOS 2.0 and later.
Declared In
UIButton.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)