The base class for controls, which are visual elements that convey a specific action or intention in response to user interactions.


class UIControl : UIView


Controls implement elements such as buttons and sliders, which your app might use to facilitate navigation, gather user input, or manipulate content. Controls use the Target-Action mechanism to report user interactions to your app.

Figure 1

Examples of UIKit controls

You do not create instances of this class directly. The UIControl class is a subclassing point that you extend to implement custom controls. You can also subclass existing control classes to extend or modify their behaviors. For example, you might override the methods of this class to track touch events yourself or to determine when the state of the control changes.

A control’s state determines its appearance and its ability to support user interactions. Controls can be in one of several states, which are defined by the UIControl.State type. You can change the state of a control programmatically based on your app’s needs. For example, you might disable a control to prevent the user from interacting with it. User interactions can also change the state of a control.

The Target-Action Mechanism

Controls use the target-action mechanism to report interesting events happening to your code. The target-action mechanism simplifies the code that you write to use controls in your app. Instead of writing code to track touch events, you write action methods to respond to control-specific events. For example, you might write an action method that responds to changes in the value of a slider. The control handles all the work of tracking incoming touch events and determining when to call your methods.

When adding an action method to a control, you specify both the action method and an object that defines that method to the addTarget(_:action:for:) method. (You can also configure the target and action of a control in Interface Builder.) The target object can be any object, but it is typically the view controller whose root view contains the control. If you specify nil for the target object, the control searches the responder chain for an object that defines the specified action method.

The signature of an action method takes one of three forms, which are listed in Listing 1. The sender parameter corresponds to the control that calls the action method, and the event parameter corresponds to the UIEvent object that triggered the control-related event.

Listing 1

Action method definitions

@IBAction func doSomething()
@IBAction func doSomething(sender: UIButton)
@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)

Action methods are called when the user interacts with the control in specific ways. The UIControl.Event type defines the types of user interactions that a control can report and those interactions mostly correlate to specific touch events within the control. When configuring a control, you must specify which events trigger the calling of your method. For a button control, you might use the touchDown or touchUpInside event to trigger calls to your action method. For a slider, you might care only about changes to the slider’s value changes and so you might choose to attach your action method to valueChanged events.

When a control-specific event occurs, the control calls any associated action methods right away. Action methods are dispatched through the current UIApplication object, which finds an appropriate object to handle the message, following the responder chain if needed. For more information about responders and the responder chain, see Event Handling Guide for UIKit Apps.

Interface Builder Attributes

Table 1 lists the attributes associated with instances of the UIControl class.

Table 1

Control attributes




The horizontal and vertical alignment of a control’s content. For controls that contain text or images, such as buttons and text fields, use these attributes to configure the position of that content within the control’s bounds.

These alignment options apply to the content of a control and not to the control itself. For information about how to align controls with respect to other controls and views, see Auto Layout Guide.


The initial state of the control. Use the checkboxes to configure whether the control is enabled, selected, or highlighted initially.


Because UIControl is an abstract class, you do not internationalize it specifically. However, you do internationalize the content of subclasses like UIButton. For information about internationalizing a specific control, see the reference for that control.


Controls are accessible by default. To be useful, an accessible user interface element must provide accurate and helpful information about its screen position, name, behavior, value, and type. This is the information VoiceOver speaks to users. Visually impaired users can rely on VoiceOver to help them use their devices.

Controls support the following accessibility attributes:

  • Label. A short, localized word or phrase that succinctly describes the control or view, but does not identify the element’s type. Examples are “Add” or “Play.”

  • Traits. A combination of one or more individual traits, each of which describes a single aspect of an element’s state, behavior, or usage. For example, an element that behaves like a keyboard key and that is currently selected can be characterized by the combination of the Keyboard Key and Selected traits.

  • Hint. A brief, localized phrase that describes the results of an action on an element. Examples are “Adds a title” or “Opens the shopping list.”

  • Frame. The frame of the element in screen coordinates, which is given by the CGRect structure that specifies an element’s screen location and size.

  • Value. The current value of an element, when the value is not represented by the label. For example, the label for a slider might be “Speed,” but its current value might be “50%.”

The UIControl class provides default content for the value and frame attributes. Many controls enable additional specific traits automatically as well. You can configure other accessibility attributes programmatically or using the Identity inspector in Interface Builder.

For more information about accessibility attributes, see Accessibility Programming Guide for iOS.

Subclassing Notes

Subclassing UIControl gives you access to the built-in target-action mechanism and simplified event-handling support. You can subclass existing controls and modify its behavior in one of two ways:

  • Override the sendAction(_:to:for:) method of an existing subclass to observe or modify the dispatching of action methods to the control’s associated targets. You might use this method to modify the dispatch behavior based on the specified object, selector, or event.

  • Override the beginTracking(_:with:), continueTracking(_:with:), endTracking(_:with:), and cancelTracking(with:) methods to track touch events occurring in the control. You can use the tracking information to perform additional actions. Always use these methods to track touch events instead of the methods defined by the UIResponder class.

If you subclass UIControl directly, your subclass is responsible for setting up and managing your control’s visual appearance. Use the methods for tracking events to update your control’s state and to send an action when the control’s value changes.


Configuring the Control’s Attributes

var state: UIControl.State

The state of the control, specified as a bitmask value.

var isEnabled: Bool

A Boolean value indicating whether the control is enabled.

var isSelected: Bool

A Boolean value indicating whether the control is in the selected state.

var isHighlighted: Bool

A Boolean value indicating whether the control draws a highlight.

var contentVerticalAlignment: UIControl.ContentVerticalAlignment

The vertical alignment of content within the control’s bounds.

enum UIControl.ContentVerticalAlignment

Constants for specifying the vertical alignment of content (text and images) in a control.

var contentHorizontalAlignment: UIControl.ContentHorizontalAlignment

The horizontal alignment of content within the control’s bounds.

var effectiveContentHorizontalAlignment: UIControl.ContentHorizontalAlignment

The horizontal alignment currently in effect for the control.

enum UIControl.ContentHorizontalAlignment

The horizontal alignment of content (text and images) within a control.

Accessing the Control’s Targets and Actions

func addTarget(Any?, action: Selector, for: UIControl.Event)

Associates a target object and action method with the control.

func removeTarget(Any?, action: Selector?, for: UIControl.Event)

Stops the delivery of events to the specified target object.

func actions(forTarget: Any?, forControlEvent: UIControl.Event) -> [String]?

Returns the actions performed on a target object when the specified event occurs.

var allControlEvents: UIControl.Event

Returns the events for which the control has associated actions.

var allTargets: Set<AnyHashable>

Returns all target objects associated with the control.

Triggering Actions

func sendAction(Selector, to: Any?, for: UIEvent?)

Calls the specified action method.

func sendActions(for: UIControl.Event)

Calls the action methods associated with the specified events.

Tracking Touches and Redrawing Controls

func beginTracking(UITouch, with: UIEvent?) -> Bool

Called when a touch event enters the control’s bounds.

func continueTracking(UITouch, with: UIEvent?) -> Bool

Called when a touch event associated with the control is updated.

func endTracking(UITouch?, with: UIEvent?)

Called when a touch event associated with the control ends.

func cancelTracking(with: UIEvent?)

Tells the control to cancel tracking related to the given event.

var isTracking: Bool

A Boolean value indicating whether the control is currently tracking touch events.

var isTouchInside: Bool

A Boolean value indicating whether a tracked touch event is currently inside the control’s bounds.


struct UIControl.Event

Constants describing the types of events possible for controls.

struct UIControl.State

Constants describing the state of a control.

See Also


class UIButton

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

class UIDatePicker

A control used for the inputting of date and time values.

class UIPageControl

A control that displays a horizontal series of dots, each of which corresponds to a page in the app’s document or other data-model entity.

class UISegmentedControl

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

class UISlider

A control used to select a single value from a continuous range of values.

class UIStepper

A control used to increment or decrement a value.

class UISwitch

A control that offers a binary choice, such as On/Off.