<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIButton",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIButton"
  },
  "title" : "UIButton"
}
-->

# UIButton

A control that executes your custom code in response to user interactions.

```
@MainActor class UIButton
```

## Overview

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.

![A screenshot showing three buttons. The first button shows the label “Button”. The second button shows an image of a plus sign in a circle. The third button shows an image of a lowercase “i” in a circle.](images/com.apple.uikit/media-2557338.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.

> Important:
> An app built with Mac Catalyst running in macOS 11 throws an exception when calling a button’s ``doc://com.apple.uikit/documentation/UIKit/UIView/addGestureRecognizer(_:)`` method when ``doc://com.apple.uikit/documentation/UIKit/UIButton/buttonType-swift.property`` is ``doc://com.apple.uikit/documentation/UIKit/UIButton/ButtonType-swift.enum/system`` and the user interface idiom is ``doc://com.apple.uikit/documentation/UIKit/UIUserInterfaceIdiom/mac``.

### Respond 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:for:)`](/documentation/UIKit/UIControl/addTarget(_:action:for:)) method or by creating a connection in Interface Builder. The signature of an action method takes one of three forms, as shown in the following code. Choose the form that provides the information that you need to respond to the button tap.

```objc
- (IBAction)doSomething;
- (IBAction)doSomething:(id)sender;
- (IBAction)doSomething:(id)sender forEvent:(UIEvent*)event;
```

### Configure 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 [`init(type:)`](/documentation/UIKit/UIButton/init(type:)) method or in your storyboard file. After creating a button, you can’t change its type. The most commonly used button types are the Custom and System types, but use the other types when appropriate.

> Note:
> To configure the appearance of all buttons in your app, use the appearance proxy object. The ``doc://com.apple.uikit/documentation/UIKit/UIButton`` class implements the ``doc://com.apple.uikit/documentation/UIKit/UIAppearance/appearance()`` class method, which you can use to fetch the appearance proxy for all buttons in your app.

#### Configure 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’s in the default state initially, which means the button is enabled and the user isn’t 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 don’t specify attributes for a particular state, the [`UIButton`](/documentation/UIKit/UIButton) class provides a reasonable default behavior. For example, a disabled button is normally dimmed and doesn’t display a highlight when tapped. Other properties of this class, such as the [`adjustsImageWhenHighlighted`](/documentation/UIKit/UIButton/adjustsImageWhenHighlighted) and [`adjustsImageWhenDisabled`](/documentation/UIKit/UIButton/adjustsImageWhenDisabled) properties, let you alter the default behavior in specific cases.

#### Provide 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`](/documentation/UIKit/UILabel) and [`UIImageView`](/documentation/UIKit/UIImageView) object managed by the button itself. You can access these objects using the [`titleLabel`](/documentation/UIKit/UIButton/titleLabel) or [`imageView`](/documentation/UIKit/UIButton/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’s possible to specify both an image and a title for buttons, which results in the appearance shown in the following image. You can access the current content of a button using the indicated properties.

![Providing a title and image for a button.](images/com.apple.uikit/media-3081016@2x.png)

When setting the content of a button, you must specify the title, image, and appearance attributes for each state separately. If you don’t 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.

#### Customize tint color

You can specify a custom button tint using the [`tintColor`](/documentation/UIKit/UIButton/tintColor) property. This property sets the color of the button image and text. If you don’t explicitly set a tint color, the button uses its superview’s tint color.

#### Specify 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`](/documentation/UIKit/UIButton/titleEdgeInsets)), image ([`imageEdgeInsets`](/documentation/UIKit/UIButton/imageEdgeInsets)), and both the title and image together ([`contentEdgeInsets`](/documentation/UIKit/UIButton/contentEdgeInsets)). When applied, insets affect the corresponding content rectangle of the button, which the Auto Layout engine uses to determine the button’s position.

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

### Configure button attributes in Interface Builder

The following table lists the core attributes that you configure for buttons in Interface Builder.

|Attribute                  |Description                                                                                                                                                                                                                                                                                                                                                                                              |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|Type                       |The button type. This attribute determines the default settings for many other button attributes. The value of this attribute can’t be changed at runtime, but you can access it using the ``doc://com.apple.uikit/documentation/UIKit/UIButton/buttonType-swift.property`` 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.                                                                                                                                                                                                                                                                                                  |

The following table lists attributes that affect the button’s appearance.

|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. ![](spacer) Configure the offsets programmatically using the ``doc://com.apple.uikit/documentation/UIKit/UILabel/shadowOffset`` property of the button’s ``doc://com.apple.uikit/documentation/UIKit/UIButton/titleLabel`` object. Configure the highlighting behavior using the ``doc://com.apple.uikit/documentation/UIKit/UIButton/reversesTitleShadowWhenHighlighted`` property.                                                                |
|Drawing      |The drawing behavior of the button. ![](spacer) When the Shows Touch On Highlight (``doc://com.apple.uikit/documentation/UIKit/UIButton/showsTouchWhenHighlighted``) option is enabled, the button adds a white glow to the part of a button that the user touches. ![](spacer) When the Highlighted Adjusts Image (``doc://com.apple.uikit/documentation/UIKit/UIButton/adjustsImageWhenHighlighted``) option is enabled, button images get darker when it’s in the highlighted state. ![](spacer) When the Disabled Adjusts Image (``doc://com.apple.uikit/documentation/UIKit/UIButton/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.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |

The following table lists the edge inset attributes for buttons. Use edge inset buttons to alter the rectangle for the button’s content.

|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 ``doc://com.apple.uikit/documentation/UIKit/UIButton/contentEdgeInsets``, ``doc://com.apple.uikit/documentation/UIKit/UIButton/titleEdgeInsets``, and ``doc://com.apple.uikit/documentation/UIKit/UIButton/imageEdgeInsets`` properties.|

For information about the button’s inherited Interface Builder attributes, see [`UIControl`](/documentation/UIKit/UIControl) and [`UIView`](/documentation/UIKit/UIView).

### Support localization

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](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/Introduction/Introduction.html#//apple_ref/doc/uid/10000171i).

### Make buttons accessible

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`](/documentation/UIKit/UIControl). For general information about making your interface accessible, see [Accessibility Programming Guide for iOS](https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/iPhoneAccessibility/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008785).

## Topics

### Creating buttons

[`init(frame:)`](/documentation/UIKit/UIButton/init(frame:))

Creates a new button with the specified frame.

[`init(frame:primaryAction:)`](/documentation/UIKit/UIButton/init(frame:primaryAction:))

Creates a new button with the specified frame, registers the primary action event, and sets the title and image to the action’s title and image.

[`init(coder:)`](/documentation/UIKit/UIButton/init(coder:))

Creates a new button with data in an unarchiver.

### Creating buttons of a specific type

[`init(type:)`](/documentation/UIKit/UIButton/init(type:))

Creates and returns a new button of the specified type.

[`init(type:primaryAction:)`](/documentation/UIKit/UIButton/init(type:primaryAction:))

Creates a new button with the specified type, registers the primary action event, and sets the title and image to the action’s title and image.

[`buttonWithType:primaryAction:`](/documentation/UIKit/UIButton/buttonWithType:primaryAction:)

Creates a new button with the specified type, registers the primary action event, and sets the title and image to the action’s title and image.

[`UIButton.ButtonType`](/documentation/UIKit/UIButton/ButtonType-swift.enum)

Specifies the style of a button.

### Creating system buttons

[`systemButtonWithPrimaryAction:`](/documentation/UIKit/UIButton/systemButtonWithPrimaryAction:)

Creates and returns a system type button, registers the primary action event, and sets the title and image to the action’s title and image.

[`systemButton(with:target:action:)`](/documentation/UIKit/UIButton/systemButton(with:target:action:))

Creates and returns a system type button with specified image, target, and action.

### Creating buttons from a configuration object

[`init(configuration:primaryAction:)`](/documentation/UIKit/UIButton/init(configuration:primaryAction:))

Creates a new button with the specified configuration and registers the primary action event.

[`buttonWithConfiguration:primaryAction:`](/documentation/UIKit/UIButton/buttonWithConfiguration:primaryAction:)

Creates a new button with the specified configuration and registers the primary action event.

[`UIButton.Configuration`](/documentation/UIKit/UIButton/Configuration-swift.struct)

A configuration that specifies the appearance and behavior of a button and its contents.

[`UIButtonConfiguration`](/documentation/UIKit/UIButtonConfiguration)

A configuration that specifies the appearance and behavior of a button and its contents.

### Managing the appearance with a configuration object

[`configuration`](/documentation/UIKit/UIButton/configuration-5rlyb)

The configuration for the button’s appearance.

[`configuration`](/documentation/UIKit/UIButton/configuration-7gz60)

The configuration for the button’s appearance.

[`automaticallyUpdatesConfiguration`](/documentation/UIKit/UIButton/automaticallyUpdatesConfiguration)

A Boolean value that determines whether the button configuration changes when button’s state changes.

[`setNeedsUpdateConfiguration()`](/documentation/UIKit/UIButton/setNeedsUpdateConfiguration())

Requests the system update the button configuration.

[`updateConfiguration()`](/documentation/UIKit/UIButton/updateConfiguration())

Updates the button configuration in response to a button state change.

[`configurationUpdateHandler`](/documentation/UIKit/UIButton/configurationUpdateHandler-swift.property)

A closure that executes when the button state changes.

[`UIButton.ConfigurationUpdateHandler`](/documentation/UIKit/UIButton/ConfigurationUpdateHandler-swift.typealias)

A closure to update the configuration of a button.

### Managing the title

[`titleLabel`](/documentation/UIKit/UIButton/titleLabel)

A view that displays the value of the `currentTitle` property for a button.

[`title(for:)`](/documentation/UIKit/UIButton/title(for:))

Returns the title associated with the specified state.

[`setTitle(_:for:)`](/documentation/UIKit/UIButton/setTitle(_:for:))

Sets the title to use for the specified state.

[`attributedTitle(for:)`](/documentation/UIKit/UIButton/attributedTitle(for:))

Returns the styled title associated with the specified state.

[`setAttributedTitle(_:for:)`](/documentation/UIKit/UIButton/setAttributedTitle(_:for:))

Sets the styled title to use for the specified state.

[`titleColor(for:)`](/documentation/UIKit/UIButton/titleColor(for:))

Returns the title color used for a state.

[`setTitleColor(_:for:)`](/documentation/UIKit/UIButton/setTitleColor(_:for:))

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

[`titleShadowColor(for:)`](/documentation/UIKit/UIButton/titleShadowColor(for:))

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

[`setTitleShadowColor(_:for:)`](/documentation/UIKit/UIButton/setTitleShadowColor(_:for:))

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

### Managing images and tint color

[`backgroundImage(for:)`](/documentation/UIKit/UIButton/backgroundImage(for:))

Returns the background image used for a button state.

[`image(for:)`](/documentation/UIKit/UIButton/image(for:))

Returns the image used for a button state.

[`setBackgroundImage(_:for:)`](/documentation/UIKit/UIButton/setBackgroundImage(_:for:))

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

[`setImage(_:for:)`](/documentation/UIKit/UIButton/setImage(_:for:))

Sets the image to use for the specified state.

[`preferredSymbolConfigurationForImage(in:)`](/documentation/UIKit/UIButton/preferredSymbolConfigurationForImage(in:))

Returns the preferred symbol configuration for a button state.

[`setPreferredSymbolConfiguration(_:forImageIn:)`](/documentation/UIKit/UIButton/setPreferredSymbolConfiguration(_:forImageIn:))

Sets the preferred symbol configuration for a button state.

[`tintColor`](/documentation/UIKit/UIButton/tintColor)

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

### Specifying the role

[`role`](/documentation/UIKit/UIButton/role-swift.property)

The role of the button.

[`UIButton.Role`](/documentation/UIKit/UIButton/Role-swift.enum)

Constants that describe the role of the button.

### Specifying the behavioral style

Determine how the button appears and behaves in your app built with Mac Catalyst.

[`behavioralStyle`](/documentation/UIKit/UIButton/behavioralStyle)

The style that determines how the button behaves.

[`preferredBehavioralStyle`](/documentation/UIKit/UIButton/preferredBehavioralStyle)

The preferred behavioral style.

[`UIBehavioralStyle`](/documentation/UIKit/UIBehavioralStyle)

Constants that indicate how a control behaves in apps built with Mac Catalyst.

### Getting the current state

[`buttonType`](/documentation/UIKit/UIButton/buttonType-swift.property)

The button type.

[`currentTitle`](/documentation/UIKit/UIButton/currentTitle)

The current title that is displayed on the button.

[`currentAttributedTitle`](/documentation/UIKit/UIButton/currentAttributedTitle)

The current styled title that is displayed on the button.

[`currentTitleColor`](/documentation/UIKit/UIButton/currentTitleColor)

The color used to display the title.

[`currentTitleShadowColor`](/documentation/UIKit/UIButton/currentTitleShadowColor)

The color of the title’s shadow.

[`currentImage`](/documentation/UIKit/UIButton/currentImage)

The current image displayed on the button.

[`currentBackgroundImage`](/documentation/UIKit/UIButton/currentBackgroundImage)

The current background image displayed on the button.

[`currentPreferredSymbolConfiguration`](/documentation/UIKit/UIButton/currentPreferredSymbolConfiguration)

The current symbol size, style, and weight.

[`imageView`](/documentation/UIKit/UIButton/imageView)

The button’s image view.

[`subtitleLabel`](/documentation/UIKit/UIButton/subtitleLabel)

The label that displays the text of the subtitle.

### Supporting pointer interactions

[`isPointerInteractionEnabled`](/documentation/UIKit/UIButton/isPointerInteractionEnabled)

A Boolean that enables pointer interaction.

[`isHovered`](/documentation/UIKit/UIButton/isHovered)

A Boolean value that indicates whether a pointer effect is active.

[`pointerStyleProvider`](/documentation/UIKit/UIButton/pointerStyleProvider-y4eb)

A closure that returns the pointer style to use when the pointer hovers over the button.

[`UIButton.PointerStyleProvider`](/documentation/UIKit/UIButton/PointerStyleProvider-swift.typealias)

A type alias defining a closure that returns a pointer style to apply to a button.

[`pointerStyleProvider`](/documentation/UIKit/UIButton/pointerStyleProvider-1d4d2)

A block that returns the pointer style to use when the pointer hovers over the button.

[`UIButtonPointerStyleProvider`](/documentation/UIKit/UIButtonPointerStyleProvider)

A type alias defining a block that returns a pointer style to apply to a button.

### Supporting menu and toggle buttons

Configure the button to display a pull-down menu, pop-up menu, or toggle a selection.

[`menu`](/documentation/UIKit/UIButton/menu)

A menu that the button displays.

[`isHeld`](/documentation/UIKit/UIButton/isHeld)

A Boolean value that indicates whether the button menu is visible.

[`changesSelectionAsPrimaryAction`](/documentation/UIKit/UIButton/changesSelectionAsPrimaryAction)

A Boolean value that indicates whether the button tracks a selection, either through a menu or a toggle.

[`preferredMenuElementOrder`](/documentation/UIKit/UIButton/preferredMenuElementOrder)

The preferred menu-element ordering strategy for the menu.

### Deprecated

[Deprecated symbols](/documentation/UIKit/uibutton-deprecated-symbols)

Symbols that buttons no longer support.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)