iOS Developer Library

Developer

UIKit Framework Reference UIButton Class Reference

Options
Deployment Target:

On This Page
Language:

UIButton

A UIButton object is a view that executes your custom code in response to user interactions. When you tap a button, or select a button that has focus, the button performs any actions attached to it. You communicate the purpose of a button using a text label, an image, or both. The appearance of buttons is configurable, so you can tint buttons or format titles to match the design of your app. You can add buttons to your interface programmatically or using Interface Builder.

image: ../Art/uibutton_intro.png

When adding a button to your interface, perform the following steps:

  • Set the type of the button at creation time.

  • Supply a title string or image; size the button appropriately for your content.

  • Connect one or more action methods to the button.

  • Set up Auto Layout rules to govern the size and position of the button in your interface.

  • Provide accessibility information and localized strings.

For information about basic view behaviors, see View Programming Guide for iOS.

Responding to Button Taps

Buttons use the target-action design pattern to notify your app when the user taps the button. Rather than handle touch events directly, you assign action methods to the button and designate which events trigger calls to your methods. At runtime, the button handles all incoming touch events and calls your methods in response.

You connect a button to your action method using the addTarget:action:forControlEvents: method or by creating a connection in Interface Builder. The signature of an action method takes one of three forms, which are listed in Listing 1. Choose the form that provides the information that you need to respond to the button tap.

Listing 1Action methods for buttons

Objective-C

  1. - (IBAction)doSomething;
  2. - (IBAction)doSomething:(id)sender;
  3. - (IBAction)doSomething:(id)sender forEvent:(UIEvent*)event;

Swift

  1. @IBAction func doSomething()
  2. @IBAction func doSomething(sender: UIButton)
  3. @IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)

Configuring a Button’s Appearance

A button’s type defines its basic appearance and behavior. You specify the type of a button at creation time using the buttonWithType: method or in your storyboard file. After creating a button, you cannot change its type. The most commonly used button types are the Custom and System types, but use the other types when appropriate.

Button States

Buttons have five states that define their appearance: default, highlighted, focused, selected, and disabled. When you add a button to your interface, it is in the default state initially, which means the button is enabled and the user is not interacting with it. As the user interacts with the button, its state changes to the other values. For example, when the user taps a button with a title, the button moves to the highlighted state.

When configuring a button either programmatically or in Interface Builder, you specify attributes for each state separately. In Interface Builder, use the State Config control in the Attributes inspector to choose the appropriate state and then configure the other attributes. If you do not specify attributes for a particular state, the UIButton class provides a reasonable default behavior. For example, a disabled button is normally dimmed and does not display a highlight when tapped. Other properties of this class, such as the adjustsImageWhenHighlighted and adjustsImageWhenDisabled properties, let you alter the default behavior in specific cases.

Content

The content of a button consists of a title string or image that you specify. The content you specify is used to configure the UILabel and UIImageView object managed by the button itself. You can access these objects using the titleLabel or imageView properties and modify their values directly. The methods of this class also provide a convenient shortcut for configuring the appearance of your string or image.

Normally, you configure a button using either a title or an image and size the button accordingly. Buttons can also have a background image, which is positioned behind the content you specify. It is possible to specify both an image and a title for buttons, which results in the appearance shown in Figure 1. You can access the current content of a button using the indicated properties.

Figure 1Providing a title and image for a button image: ../Art/uibutton_callouts_2x.png

When setting the content of a button, you must specify the title, image, and appearance attributes for each state separately. If you do not customize the content for a particular state, the button uses the values associated with the Default state and adds any appropriate customizations. For example, in the highlighted state, an image-based button draws a highlight on top of the default image if no custom image is provided.

Tint Color

You can specify a custom button tint using the tintColor property. This property sets the color of the button image and text. If you do not explicitly set a tint color, the button uses its superview’s tint color.

Edge Insets

Use insets to add or remove space around the content in your custom or system buttons. You can specify separate insets for your button’s title (titleEdgeInsets), image (imageEdgeInsets), and both the title and image together (contentEdgeInsets). When applied, insets affect the corresponding content rectangle of the button, which is used by the Auto Layout engine to determine the button’s position.

There should be no reason for you to adjust the edge insets for info, contact, or disclosure buttons.

Interface Builder Attributes

Table 1 lists the core attributes that you configure for buttons in Interface Builder.

Table 1Core attributes

Attribute

Description

Type

The button type. This attribute determines the default settings for many other button attributes. The value of this attribute cannot be changed at runtime, but you can access it using the buttonType property.

State Config

The state selector. After selecting a value in this control, changes to the button’s attributes apply to the specified state.

Title

The button’s title. You can specify a button’s title as a plain string or attributed string.

(Title Font and Attributes)

The font and other attributes to apply to the button’s title string. The specific configuration options depends on whether you specified a plain string or attributed string for the button’s title. For a plain string, you can customize the font, text color, and shadow color. For an attributed string, you can specify alignment, text direction, indentation, hyphenation, and many other options.

Image

The button’s foreground image. Typically, you use template images for a button’s foreground, but you may specify any image in your Xcode project.

Background

The button’s background image. The background image is displayed behind its title and foreground image.

Table 2 lists attributes that affect the button’s appearance.

Table 2Appearance attributes

Attribute

Description

Shadow Offset

The offsets and behavior of the button’s shadow. Shadows affect title strings only. Enable the Reverses on Highlight option to change the highlighting of the shadow when the button state changes to or from the highlighted state.

Configure the offsets programmatically using the shadowOffset property of the button’s titleLabel object. Configure the highlighting behavior using the reversesTitleShadowWhenHighlighted property.

Drawing

The drawing behavior of the button.

When the Shows Touch On Highlight (showsTouchWhenHighlighted) option is enabled, the button adds a white glow to the part of a button that the user touches.

When the Highlighted Adjusts Image (adjustsImageWhenHighlighted) option is enabled, button images get darker when it is in the highlighted state.

When the Disabled Adjusts Image (adjustsImageWhenDisabled) option is enabled, the image is dimmed when the button is disabled.

Line Break

The line breaking options for the button’s text. Use this attribute to define how the button’s title is modified to fit the available space.

Table 3 lists the edge inset attributes for buttons. Use edge inset buttons to alter the rectangle for the button’s content.

Table 3Edge inset attributes

Attribute

Description

Edge

The edge insets to configure. You can specify separate edge insets for the button’s overall content, its title, and its image.

Inset

The inset values. Positive values shrink the corresponding edge, moving it closer to the center of the button. Negative values expand the edge, moving it away from the center of the button. Access these values at runtime using the contentEdgeInsets, titleEdgeInsets, and imageEdgeInsets properties.

For information about the button’s inherited Interface Builder attributes, see UIControl Class Reference and UIView Class Reference.

Internationalization

To internationalize a button, specify a localized string for the button’s title text. (You may also localize a button’s image as appropriate.)

When using storyboards to build your interface, use Xcode’s base internationalization feature to configure the localizations your project supports. When you add a localization, Xcode creates a strings file for that localization. When configuring your interface programmatically, use the system’s built-in support for loading localized strings and resources. For more information about internationalizing your interface, see Internationalization and Localization Guide.

Accessibility

Buttons are accessible by default. The default accessibility traits for a button are Button and User Interaction Enabled.

The accessibility label, traits, and hint are spoken back to the user when VoiceOver is enabled on a device. The button’s title overwrites its accessibility label; even if you set a custom value for the label, VoiceOver speaks the value of the title. VoiceOver speaks this information when a user taps the button once. For example, when a user taps the Options button in Camera, VoiceOver speaks the following:

  • "Options. Button. Shows additional camera options."

For more information about making iOS controls accessible, see the accessibility information in UIControl Class Reference. For general information about making your interface accessible, see Accessibility Programming Guide for iOS.

  • Creates and returns a new button of the specified type.

    Declaration

    Swift

    convenience init(type buttonType: UIButtonType)

    Objective-C

    + (instancetype)buttonWithType:(UIButtonType)buttonType

    Parameters

    buttonType

    The button type. See UIButtonType for the possible values.

    Return Value

    A newly created button.

    Discussion

    This method is a convenience constructor for creating button objects with specific configurations. If 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.

  • A view that displays the value of the currentTitle property for a button. (read-only)

    Declaration

    Swift

    var titleLabel: UILabel? { get }

    Objective-C

    @property(nonatomic, readonly, strong) UILabel *titleLabel

    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:

    1. UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
    2. button.titleLabel.font = [UIFont systemFontOfSize: 12];
    3. button.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;

    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

    currentTitle

  • Returns the title associated with the specified state.

    Declaration

    Swift

    func titleForState(_ state: UIControlState) -> String?

    Objective-C

    - (NSString *)titleForState:(UIControlState)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.

  • Sets the title to use for the specified state.

    Declaration

    Swift

    func setTitle(_ title: String?, forState state: UIControlState)

    Objective-C

    - (void)setTitle:(NSString *)title forState:(UIControlState)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.

  • Returns the styled title associated with the specified state.

    Declaration

    Swift

    func attributedTitleForState(_ state: UIControlState) -> NSAttributedString?

    Objective-C

    - (NSAttributedString *)attributedTitleForState:(UIControlState)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. If no attributed title has been set for UIControlStateNormal, returns nil.

    Availability

    Available in iOS 6.0 and later.

  • Sets the styled title to use for the specified state.

    Declaration

    Swift

    func setAttributedTitle(_ title: NSAttributedString?, forState state: UIControlState)

    Objective-C

    - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)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.

  • Returns the title color used for a state.

    Declaration

    Swift

    func titleColorForState(_ state: UIControlState) -> UIColor?

    Objective-C

    - (UIColor *)titleColorForState:(UIControlState)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.

  • Sets the color of the title to use for the specified state.

    Declaration

    Swift

    func setTitleColor(_ color: UIColor?, forState state: UIControlState)

    Objective-C

    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)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.

  • Returns the shadow color of the title used for a state.

    Declaration

    Swift

    func titleShadowColorForState(_ state: UIControlState) -> UIColor?

    Objective-C

    - (UIColor *)titleShadowColorForState:(UIControlState)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.

  • Sets the color of the title shadow to use for the specified state.

    Declaration

    Swift

    func setTitleShadowColor(_ color: UIColor?, forState state: UIControlState)

    Objective-C

    - (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)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.

  • A Boolean value that determines whether the title shadow changes when the button is highlighted.

    Declaration

    Swift

    var reversesTitleShadowWhenHighlighted: Bool

    Objective-C

    @property(nonatomic) BOOL reversesTitleShadowWhenHighlighted

    Discussion

    If YEStrue, the shadow changes from engrave to emboss appearance when highlighted. The default value is NOfalse.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value that determines whether the image changes when the button is highlighted.

    Declaration

    Swift

    var adjustsImageWhenHighlighted: Bool

    Objective-C

    @property(nonatomic) BOOL adjustsImageWhenHighlighted

    Discussion

    If YEStrue, the image is drawn lighter when the button is highlighted. The default value is YEStrue.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value that determines whether the image changes when the button is disabled.

    Declaration

    Swift

    var adjustsImageWhenDisabled: Bool

    Objective-C

    @property(nonatomic) BOOL adjustsImageWhenDisabled

    Discussion

    If YEStrue, the image is drawn darker when the button is disabled. The default value is YEStrue.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value that determines whether tapping the button causes it to glow.

    Declaration

    Swift

    var showsTouchWhenHighlighted: Bool

    Objective-C

    @property(nonatomic) BOOL showsTouchWhenHighlighted

    Discussion

    If YEStrue, the button glows when tapped; otherwise, it does not. The image and button behavior is not changed by the glow. The default value is NOfalse.

    Availability

    Available in iOS 2.0 and later.

  • Returns the background image used for a button state.

    Declaration

    Swift

    func backgroundImageForState(_ state: UIControlState) -> UIImage?

    Objective-C

    - (UIImage *)backgroundImageForState:(UIControlState)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.

  • Returns the image used for a button state.

    Declaration

    Swift

    func imageForState(_ state: UIControlState) -> UIImage?

    Objective-C

    - (UIImage *)imageForState:(UIControlState)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.

  • Sets the background image to use for the specified button state.

    Declaration

    Swift

    func setBackgroundImage(_ image: UIImage?, forState state: UIControlState)

    Objective-C

    - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)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.

  • Sets the image to use for the specified state.

    Declaration

    Swift

    func setImage(_ image: UIImage?, forState state: UIControlState)

    Objective-C

    - (void)setImage:(UIImage *)image forState:(UIControlState)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.

  • The tint color to apply to the button title and image.

    Declaration

    Swift

    var tintColor: UIColor!

    Objective-C

    @property(nonatomic, strong) 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.

    This property has no default effect for buttons with type UIButtonTypeCustom. For custom buttons, you must implement any behavior related to tintColor yourself.

    Availability

    Available in iOS 5.0 and later.

  • The inset or outset margins for the rectangle surrounding all of the button’s content.

    Declaration

    Swift

    var contentEdgeInsets: UIEdgeInsets

    Objective-C

    @property(nonatomic) UIEdgeInsets contentEdgeInsets

    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.

    The button uses this property to determine intrinsicContentSize and sizeThatFits:.

    Availability

    Available in iOS 2.0 and later.

  • The inset or outset margins for the rectangle around the button’s title text.

    Declaration

    Swift

    var titleEdgeInsets: UIEdgeInsets

    Objective-C

    @property(nonatomic) UIEdgeInsets titleEdgeInsets

    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.

    This property is used only for positioning the title during layout. The button does not use this property to determine intrinsicContentSize and sizeThatFits:.

    Availability

    Available in iOS 2.0 and later.

  • The inset or outset margins for the rectangle around the button’s image.

    Declaration

    Swift

    var imageEdgeInsets: UIEdgeInsets

    Objective-C

    @property(nonatomic) UIEdgeInsets imageEdgeInsets

    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.

    This property is used only for positioning the image during layout. The button does not use this property to determine intrinsicContentSize and sizeThatFits:.

    Availability

    Available in iOS 2.0 and later.

  • The button type. (read-only)

    Declaration

    Swift

    var buttonType: UIButtonType { get }

    Objective-C

    @property(nonatomic, readonly) UIButtonType buttonType

    Discussion

    See UIButtonType for the possible values.

    Availability

    Available in iOS 2.0 and later.

  • The current title that is displayed on the button. (read-only)

    Declaration

    Swift

    var currentTitle: String? { get }

    Objective-C

    @property(nonatomic, readonly, strong) NSString *currentTitle

    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.

  • The current styled title that is displayed on the button. (read-only)

    Declaration

    Swift

    var currentAttributedTitle: NSAttributedString? { get }

    Objective-C

    @property(nonatomic, readonly, strong) NSAttributedString *currentAttributedTitle

    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.

  • The color used to display the title. (read-only)

    Declaration

    Swift

    var currentTitleColor: UIColor { get }

    Objective-C

    @property(nonatomic, readonly, strong) UIColor *currentTitleColor

    Discussion

    This value is guaranteed not to be nil. The default value is white.

    Availability

    Available in iOS 2.0 and later.

  • The color of the title’s shadow. (read-only)

    Declaration

    Swift

    var currentTitleShadowColor: UIColor? { get }

    Objective-C

    @property(nonatomic, readonly, strong) UIColor *currentTitleShadowColor

    Discussion

    The default value is white.

    Availability

    Available in iOS 2.0 and later.

  • The current image displayed on the button. (read-only)

    Declaration

    Swift

    var currentImage: UIImage? { get }

    Objective-C

    @property(nonatomic, readonly, strong) UIImage *currentImage

    Discussion

    This value can be nil.

    Availability

    Available in iOS 2.0 and later.

  • The current background image displayed on the button. (read-only)

    Declaration

    Swift

    var currentBackgroundImage: UIImage? { get }

    Objective-C

    @property(nonatomic, readonly, strong) UIImage *currentBackgroundImage

    Discussion

    This value can be nil.

    Availability

    Available in iOS 2.0 and later.

    See Also

    currentImage

  • The button’s image view. (read-only)

    Declaration

    Swift

    var imageView: UIImageView? { get }

    Objective-C

    @property(nonatomic, readonly, strong) UIImageView *imageView

    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:

    1. UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
    2. 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.

  • Returns the rectangle in which the receiver draws its background.

    Declaration

    Swift

    func backgroundRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)backgroundRectForBounds:(CGRect)bounds

    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.

  • Returns the rectangle in which the receiver draws its entire content.

    Declaration

    Swift

    func contentRectForBounds(_ bounds: CGRect) -> CGRect

    Objective-C

    - (CGRect)contentRectForBounds:(CGRect)bounds

    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.

  • Returns the rectangle in which the receiver draws its title.

    Declaration

    Swift

    func titleRectForContentRect(_ contentRect: CGRect) -> CGRect

    Objective-C

    - (CGRect)titleRectForContentRect:(CGRect)contentRect

    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.

  • Returns the rectangle in which the receiver draws its image.

    Declaration

    Swift

    func imageRectForContentRect(_ contentRect: CGRect) -> CGRect

    Objective-C

    - (CGRect)imageRectForContentRect:(CGRect)contentRect

    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.

  • font (iOS 3.0) Property

    The font used to display text on the button.

    Deprecation Statement

    Use the font property of the titleLabel instead.

    Declaration

    Objective-C

    @property(nonatomic, strong) UIFont *font

    Discussion

    If nil, a system font is used. The default value is nil.

    Availability

    Available in iOS 2.0 and later.

    Deprecated in iOS 3.0.

    See Also

    titleLabel

  • lineBreakMode (iOS 3.0) Property

    The line break mode to use when drawing text.

    Deprecation Statement

    Use the lineBreakMode property of the titleLabel instead.

    Declaration

    Objective-C

    @property(nonatomic) NSLineBreakMode lineBreakMode

    Discussion

    This property is one of the constants described in the UILineBreakMode enumeration in NSString UIKit Additions Reference. The default value is UILineBreakModeMiddleTruncation.

    Availability

    Available in iOS 2.0 and later.

    Deprecated in iOS 3.0.

    See Also

    titleLabel

  • titleShadowOffset (iOS 3.0) Property

    The offset of the shadow used to display the receiver’s title.

    Deprecation Statement

    Use the shadowOffset property of the titleLabel instead.

    Declaration

    Objective-C

    @property(nonatomic) CGSize titleShadowOffset

    Discussion

    The horizontal and vertical offset values, specified using the width and height fields of the CGSize data type. Positive values always extend up and to the right from the user's perspective. The default value is CGSizeZero.

    Availability

    Available in iOS 2.0 and later.

    Deprecated in iOS 3.0.

    See Also

    titleLabel

Data Types

  • Specifies the style of a button.

    Declaration

    Swift

    enum UIButtonType : Int { case Custom case System case DetailDisclosure case InfoLight case InfoDark case ContactAdd static var RoundedRect: UIButtonType { get } }

    Objective-C

    typedef enum { UIButtonTypeCustom = 0, UIButtonTypeSystem, UIButtonTypeDetailDisclosure, UIButtonTypeInfoLight, UIButtonTypeInfoDark, UIButtonTypeContactAdd, UIButtonTypeRoundedRect, } UIButtonType;

    Constants

    • custom

      UIButtonTypeCustom

      No button style.

      Available in iOS 2.0 and later.

    • system

      UIButtonTypeSystem

      A system style button, such as those shown in navigation bars and toolbars.

      Available in iOS 7.0 and later.

    • detailDisclosure

      UIButtonTypeDetailDisclosure

      A detail disclosure button.

      Available in iOS 2.0 and later.

    • infoLight

      UIButtonTypeInfoLight

      An information button that has a light background.

      Available in iOS 2.0 and later.

    • infoDark

      UIButtonTypeInfoDark

      An information button that has a dark background.

      Available in iOS 2.0 and later.

    • contactAdd

      UIButtonTypeContactAdd

      A contact add button.

      Available in iOS 2.0 and later.

    • roundedRect

      UIButtonTypeRoundedRect

      A rounded-rectangle style button.

      Use UIButtonTypeSystem instead.

      Deprecated in iOS 7.0.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.