iOS Developer Library

Developer

UIKit Framework Reference UIControl Class Reference

Options
Deployment Target:

On This Page
Language:

UIControl

The UIControl class implements common behavior for visual elements that convey a specific action or intention in response to user interactions. 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 1Examples of UIKit controls image: ../Art/uicontrol_intro_2x.png

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 UIControlState 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:forControlEvents: 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 1Action method definitions

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)

Action methods are called when the user interacts with the control in specific ways. The UIControlEvents 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 UIControlEventTouchDown or UIControlEventTouchUpInside 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 UIControlEventValueChanged 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 iOS.

Interface Builder Attributes

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

Table 1Control attributes

Attribute

Description

Alignment

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.

Content

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

Internationalization

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.

Accessibility

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:

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.

  • The state of the control, specified as a bitmask value. (read-only)

    Declaration

    Swift

    var state: UIControlState { get }

    Objective-C

    @property(nonatomic, readonly) UIControlState state

    Discussion

    The value of this property is a bitmask of the constants in the UIControlState type. A control can be in more than one state at a time. For example, it can be focused and highlighted at the same time. You can also get the values for individual states using the properties of this class.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether the control is enabled.

    Declaration

    Swift

    var enabled: Bool

    Objective-C

    @property(nonatomic, getter=isEnabled) BOOL enabled

    Discussion

    Set the value of this property to YEStrue to enable the control or NOfalse to disable it. An enabled control is capable of responding to user interactions, whereas a disabled control ignores touch events and may draw itself differently. Setting this property to NOfalse adds the UIControlStateDisabled flag to the control’s state bitmask; enabling the control again removes that flag.

    The default value of this property is YEStrue for a newly created control. You can set a control’s initial enabled state in your storyboard file.

    Availability

    Available in iOS 2.0 and later.

    See Also

    state

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

    Declaration

    Swift

    var selected: Bool

    Objective-C

    @property(nonatomic, getter=isSelected) BOOL selected

    Discussion

    Set the value of this property to YEStrue to select it or NOfalse to deselect it. Most controls do not modify their appearance or behavior when selected, but some do. For example, the UISegmentedControl class tracks whether a segment is selected and draws it differently when it is.

    The default value of this property is NOfalse for a newly created control. You can set a control’s initial selected state in your storyboard file.

    Availability

    Available in iOS 2.0 and later.

    See Also

    state

  • A Boolean value indicating whether the control draws a highlight.

    Declaration

    Swift

    var highlighted: Bool

    Objective-C

    @property(nonatomic, getter=isHighlighted) BOOL highlighted

    Discussion

    When the value of this property is YEStrue, the control draws a highlight; otherwise, the control does not draw a highlight. Controls automatically set and clear this state in response to appropriate touch events. You can change the value of this property as needed to apply or remove a highlight programmatically

    The default value of this property is NOfalse for a newly created control. You can set a control’s initial selected state in your storyboard file.

    Availability

    Available in iOS 2.0 and later.

    See Also

    state

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

    Declaration

    Swift

    var contentVerticalAlignment: UIControlContentVerticalAlignment

    Objective-C

    @property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment

    Parameters

    contentAlignment

    A constant that specifies the vertical alignment of text or images within the control. For a list of possible values, see UIControlContentVerticalAlignment.

    Discussion

    For controls that contain configurable text or image content, use this property to align that content appropriately inside the control’s bounds. Not all control subclasses have content that can be aligned, and it is the responsibility of the subclass to determine how to apply this value. The default value of this property is UIControlContentVerticalAlignmentTop.

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    var contentHorizontalAlignment: UIControlContentHorizontalAlignment

    Objective-C

    @property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment

    Parameters

    contentAlignment

    A constant that specifies the horizontal alignment of text or images within the control. For a list of possible values, see UIControlContentHorizontalAlignment.

    Discussion

    For controls that contain configurable text or image content, use this property to align that content appropriately inside the control’s bounds. Not all control subclasses have content that can be aligned, and it is the responsibility of the subclass to determine how to apply this value. The default value of this property is UIControlContentHorizontalAlignmentLeft.

    Availability

    Available in iOS 2.0 and later.

  • Associates a target object and action method with the control.

    Declaration

    Swift

    func addTarget(_ target: AnyObject?, action action: Selector, forControlEvents controlEvents: UIControlEvents)

    Objective-C

    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

    Parameters

    target

    The target object—that is, the object whose action method is called. If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object.

    action

    A selector identifying the action method to be called. You may specify a selector whose signature matches any of the signatures in Listing 1. This parameter must not be nil.

    controlEvents

    A bitmask specifying the control-specific events for which the action method is called. Always specify at least one constant. For a list of possible constants, see UIControlEvents.

    Discussion

    You may call this method multiple times to configure multiple targets and actions for the control. It is also safe to call this method multiple times with the same values for the target and action parameters. The control maintains a list of its attached targets and actions along and the events each supports.

    The control does not retain the object in the target parameter. It is your responsibility to maintain a strong reference to the target object while it is attached to a control.

    Specifying a value of 0 for the controlEvents parameter does not prevent events from being sent to a previously registered target and action method. To stop the delivery of events, always call the removeTarget:action:forControlEvents: method.

    Availability

    Available in iOS 2.0 and later.

  • Stops the delivery of events to the specified target object.

    Declaration

    Swift

    func removeTarget(_ target: AnyObject?, action action: Selector, forControlEvents controlEvents: UIControlEvents)

    Objective-C

    - (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

    Parameters

    target

    A target object registered with the control. Specify nil to remove the specified control events for all target objects.

    action

    A selector identifying a registered action method. You may specify nil for this parameter.

    controlEvents

    A bitmask specifying the control events that you want to remove for the specified target object. For a list of possible constants, see UIControlEvents.

    Discussion

    Use this method to prevent the delivery of control events to target objects associated with control. If you specify a valid object in the target parameter, this method stops the delivery of the specified events to all action methods associated with that object. If you specify nil for the target parameter, this method prevents the delivery of those events to all action methods of all target objects.

    Although the action parameter is not considered when stopping the delivery of events, you should specify an appropriate value anyway. If the specified target/action combination no longer has any valid control events associated with it, the control cleans up its corresponding internal data structures. Doing so can affect the set of objects returned by the allTargets method.

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func actionsForTarget(_ target: AnyObject?, forControlEvent controlEvent: UIControlEvents) -> [String]?

    Objective-C

    - (NSArray<NSString *> *)actionsForTarget:(id)target forControlEvent:(UIControlEvents)controlEvent

    Parameters

    target

    The target object—that is, an object that has an action method associated with this control. You must pass an explicit object for this method to return a meaningful result. Specifying nil always returns nil.

    controlEvent

    A single control event constant representing the event for which you want the list of action methods. For a list of possible constants, see UIControlEvents

    Return Value

    An array NSString objects containing the selector names of the corresponding action methods, or nil if there are no action methods associated with the specified target object and control event.

    Discussion

    Use this method to determine what action methods are called on the specified object in response to a particular control event. You can use the NSSelectorFromString function to convert the returned strings to valid selectors, as needed.

    Availability

    Available in iOS 2.0 and later.

  • Returns the events for which the control has associated actions.

    Declaration

    Swift

    func allControlEvents() -> UIControlEvents

    Objective-C

    - (UIControlEvents)allControlEvents

    Return Value

    A bitmask of constants indicating the events for which this control has associated actions. For a list of possible constants, see the UIControlEvents type.

    Discussion

    You can use this method to ascertain which control events trigger actions. More than one action method may be called for a given event.

    Availability

    Available in iOS 2.0 and later.

    See Also

    – allTargets

  • Returns all target objects associated with the control.

    Declaration

    Swift

    func allTargets() -> Set<NSObject>

    Objective-C

    - (NSSet *)allTargets

    Return Value

    A set of all target objects associated with the control. The returned set may include one or more NSNull objects to indicate actions that are dispatched to the responder chain.

    Availability

    Available in iOS 2.0 and later.

  • Calls the specified action method.

    Declaration

    Swift

    func sendAction(_ action: Selector, to target: AnyObject?, forEvent event: UIEvent?)

    Objective-C

    - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

    Parameters

    action

    A selector identifying the action method to call. This parameter must not be nil.

    target

    The target object—that is, the object that implements the specified action. Specify nil if you want the app to search the responder chain for an object capable of performing the action.

    event

    The event that triggered the calling of the action method. You may specify nil for this parameter if you are calling this method directly, instead of in response to an event. For example, you might specify nil when changing the value of a control programmatically.

    Discussion

    This method takes the provided information and forwards it to the singleton UIApplication object for dispatching. If a valid target object was supplied, the app calls the action method on that target object. If the target object is nil, the app searches the responder chain for an object that defines the method.

    Subclasses may override this method and use it to observe or modify the action dispatching behavior. Implementations should call super when they want to continue with the execution of the action method.

    Availability

    Available in iOS 2.0 and later.

  • Calls the action methods associated with the specified events.

    Declaration

    Swift

    func sendActionsForControlEvents(_ controlEvents: UIControlEvents)

    Objective-C

    - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents

    Parameters

    controlEvents

    A bitmask whose set flags specify the control events for which action messages are sent. See UIControlEvents for bitmask constants.

    Discussion

    You call this method when you want the control to perform the actions associated with the specified events. This method iterates over the control’s registered targets and action methods and calls the sendAction:to:forEvent: method for each one that is associated with an event in the controlEvents parameter.

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func beginTrackingWithTouch(_ touch: UITouch, withEvent event: UIEvent?) -> Bool

    Objective-C

    - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

    Parameters

    touch

    The object containing information about the touch event.

    event

    The event object containing the touch event.

    Return Value

    YEStrue if the control should continue tracking touch events or NOfalse if it should stop. This value is used to update the tracking property of the control.

    Discussion

    The default implementation of this method always returns YEStrue. Subclasses can override this method and use it to respond to events. Use the provided event information to detect which part of your control was hit and to set up any initial state information. If you want to continue tracking the touch event, return YEStrue. If you want to stop tracking the touch event, return NOfalse.

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func continueTrackingWithTouch(_ touch: UITouch, withEvent event: UIEvent?) -> Bool

    Objective-C

    - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

    Parameters

    touch

    The touch object containing updated information.

    event

    The event object containing the touch event.

    Return Value

    YEStrue if the control should continue tracking touch events or NOfalse if it should stop. This value is used to update the tracking property of the control.

    Discussion

    This method is called repeatedly while a touch event is being tracked inside the control’s bounds. The default implementation of this method always returns YEStrue. Subclasses can override this method and use it to update their state based on changes to the touch event. If you want to continue tracking the touch event, return YEStrue. If you want to stop tracking the touch event, return NOfalse.

    Availability

    Available in iOS 2.0 and later.

  • Called when a touch event associated with the control ends.

    Declaration

    Swift

    func endTrackingWithTouch(_ touch: UITouch?, withEvent event: UIEvent?)

    Objective-C

    - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event

    Parameters

    touch

    The touch object containing the final touch information.

    event

    The event object containing the touch event.

    Discussion

    This method is called at the end of a sequence of touch events inside the control’s bounds. Subclasses can override this method and use it to perform any actions relevant to the completion of the touch sequence. You should also use it to perform any cleanup associated with tracking the event.

    If you override this method, you must call super at some point in your implementation. The default implementation updates the tracking property of the control.

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func cancelTrackingWithEvent(_ event: UIEvent?)

    Objective-C

    - (void)cancelTrackingWithEvent:(UIEvent *)event

    Parameters

    event

    An event object related to touches that occurred in the control. This parameter might be nil, indicating that the cancelation was caused by something other than an event, such as the view being removed from the window.

    Discussion

    The control calls this method when a control-related touch event is cancelled. The default implementation cancels any ongoing tracking and updates the control’s state information. Subclasses can override this method and use it to perform any actions relevant to the cancellation of the touch sequence. You should also use it to perform any cleanup associated with tracking the event.

    If you override this method, you must call super at some point in your implementation.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether the control is currently tracking touch events. (read-only)

    Declaration

    Swift

    var tracking: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isTracking) BOOL tracking

    Discussion

    While tracking of a touch event is in progress, the control sets the value of this property to YEStrue. When tracking ends or is cancelled for any reason, it sets this property to NOfalse.

    Availability

    Available in iOS 2.0 and later.

  • A Boolean value indicating whether a tracked touch event is currently inside the control’s bounds. (read-only)

    Declaration

    Swift

    var touchInside: Bool { get }

    Objective-C

    @property(nonatomic, readonly, getter=isTouchInside) BOOL touchInside

    Return Value

    YEStrue if the location of the most recent touch event is inside the control’s bounds or NOfalse if it is not.

    Discussion

    While tracking of a touch event is ongoing, the control updates the value of this property to indicate whether the most recent touch is still inside the control’s bounds. The control uses this information to trigger specific events. For example, touch events entering or exiting a control trigger appropriate drag events.

    Availability

    Available in iOS 2.0 and later.

  • Constants describing the types of events possible for controls.

    Declaration

    Swift

    struct UIControlEvents : OptionSetType { init(rawValue rawValue: UInt) static var TouchDown: UIControlEvents { get } static var TouchDownRepeat: UIControlEvents { get } static var TouchDragInside: UIControlEvents { get } static var TouchDragOutside: UIControlEvents { get } static var TouchDragEnter: UIControlEvents { get } static var TouchDragExit: UIControlEvents { get } static var TouchUpInside: UIControlEvents { get } static var TouchUpOutside: UIControlEvents { get } static var TouchCancel: UIControlEvents { get } static var ValueChanged: UIControlEvents { get } static var PrimaryActionTriggered: UIControlEvents { get } static var EditingDidBegin: UIControlEvents { get } static var EditingChanged: UIControlEvents { get } static var EditingDidEnd: UIControlEvents { get } static var EditingDidEndOnExit: UIControlEvents { get } static var AllTouchEvents: UIControlEvents { get } static var AllEditingEvents: UIControlEvents { get } static var ApplicationReserved: UIControlEvents { get } static var SystemReserved: UIControlEvents { get } static var AllEvents: UIControlEvents { get } }

    Objective-C

    typedef enum UIControlEvents : NSUInteger { UIControlEventTouchDown = 1 << 0, UIControlEventTouchDownRepeat = 1 << 1, UIControlEventTouchDragInside = 1 << 2, UIControlEventTouchDragOutside = 1 << 3, UIControlEventTouchDragEnter = 1 << 4, UIControlEventTouchDragExit = 1 << 5, UIControlEventTouchUpInside = 1 << 6, UIControlEventTouchUpOutside = 1 << 7, UIControlEventTouchCancel = 1 << 8, UIControlEventValueChanged = 1 << 12, UIControlEventPrimaryActionTriggered = 1 << 13, UIControlEventEditingDidBegin = 1 << 16, UIControlEventEditingChanged = 1 << 17, UIControlEventEditingDidEnd = 1 << 18, UIControlEventEditingDidEndOnExit = 1 << 19, UIControlEventAllTouchEvents = 0x00000FFF, UIControlEventAllEditingEvents = 0x000F0000, UIControlEventApplicationReserved = 0x0F000000, UIControlEventSystemReserved = 0xF0000000, UIControlEventAllEvents = 0xFFFFFFFF } UIControlEvents;

    Constants

    • touchDown

      UIControlEventTouchDown

      A touch-down event in the control.

      Available in iOS 2.0 and later.

    • touchDownRepeat

      UIControlEventTouchDownRepeat

      A repeated touch-down event in the control; for this event the value of the UITouch tapCount method is greater than one.

      Available in iOS 2.0 and later.

    • touchDragInside

      UIControlEventTouchDragInside

      An event where a finger is dragged inside the bounds of the control.

      Available in iOS 2.0 and later.

    • touchDragOutside

      UIControlEventTouchDragOutside

      An event where a finger is dragged just outside the bounds of the control.

      Available in iOS 2.0 and later.

    • touchDragEnter

      UIControlEventTouchDragEnter

      An event where a finger is dragged into the bounds of the control.

      Available in iOS 2.0 and later.

    • touchDragExit

      UIControlEventTouchDragExit

      An event where a finger is dragged from within a control to outside its bounds.

      Available in iOS 2.0 and later.

    • touchUpInside

      UIControlEventTouchUpInside

      A touch-up event in the control where the finger is inside the bounds of the control.

      Available in iOS 2.0 and later.

    • touchUpOutside

      UIControlEventTouchUpOutside

      A touch-up event in the control where the finger is outside the bounds of the control.

      Available in iOS 2.0 and later.

    • touchCancel

      UIControlEventTouchCancel

      A system event canceling the current touches for the control.

      Available in iOS 2.0 and later.

    • valueChanged

      UIControlEventValueChanged

      A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.

      Available in iOS 2.0 and later.

    • primaryActionTriggered

      UIControlEventPrimaryActionTriggered

      A semantic action triggered by buttons.

      Available in iOS 9.0 and later.

    • editingDidBegin

      UIControlEventEditingDidBegin

      A touch initiating an editing session in a UITextField object by entering its bounds.

      Available in iOS 2.0 and later.

    • editingChanged

      UIControlEventEditingChanged

      A touch making an editing change in a UITextField object.

      Available in iOS 2.0 and later.

    • editingDidEnd

      UIControlEventEditingDidEnd

      A touch ending an editing session in a UITextField object by leaving its bounds.

      Available in iOS 2.0 and later.

    • editingDidEndOnExit

      UIControlEventEditingDidEndOnExit

      A touch ending an editing session in a UITextField object.

      Available in iOS 2.0 and later.

    • allTouchEvents

      UIControlEventAllTouchEvents

      All touch events.

      Available in iOS 2.0 and later.

    • allEditingEvents

      UIControlEventAllEditingEvents

      All editing touches for UITextField objects.

      Available in iOS 2.0 and later.

    • applicationReserved

      UIControlEventApplicationReserved

      A range of control-event values available for application use.

      Available in iOS 2.0 and later.

    • systemReserved

      UIControlEventSystemReserved

      A range of control-event values reserved for internal framework use.

      Available in iOS 2.0 and later.

    • allEvents

      UIControlEventAllEvents

      All events, including system events.

      Available in iOS 2.0 and later.

    Discussion

    You set up a control so that it sends an action message to a target object by associating both target and action with one or more control events. To do this, send addTarget:action:forControlEvents:: to the control for each target-action pair you want to specify.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

  • Constants describing the state of a control.

    Declaration

    Swift

    struct UIControlState : OptionSetType { init(rawValue rawValue: UInt) static var Normal: UIControlState { get } static var Highlighted: UIControlState { get } static var Disabled: UIControlState { get } static var Selected: UIControlState { get } static var Focused: UIControlState { get } static var Application: UIControlState { get } static var Reserved: UIControlState { get } }

    Objective-C

    typedef enum UIControlState : NSUInteger { UIControlStateNormal = 0, UIControlStateHighlighted = 1 << 0, UIControlStateDisabled = 1 << 1, UIControlStateSelected = 1 << 2, UIControlStateFocused = 1 << 3, UIControlStateApplication = 0x00FF0000, UIControlStateReserved = 0xFF000000 } UIControlState;

    Constants

    • normal

      UIControlStateNormal

      The normal, or default state of a control—that is, enabled but neither selected nor highlighted.

      Available in iOS 2.0 and later.

    • highlighted

      UIControlStateHighlighted

      Highlighted state of a control. A control becomes highlighted when a touch event enters the control’s bounds, and it loses that highlight when there is a touch-up event or when the touch event exits the control’s bounds. You can retrieve and set this value through the highlighted property.

      Available in iOS 2.0 and later.

    • disabled

      UIControlStateDisabled

      Disabled state of a control. User interactions with disabled control have no effect and the control draws itself with a dimmed appearance to reflect that it is disabled. You can retrieve and set this value through the enabled property.

      Available in iOS 2.0 and later.

    • selected

      UIControlStateSelected

      Selected state of a control. For many controls, this state has no effect on behavior or appearance. Some subclasses, like the UISegmentedControl class, use this state to change their appearance. You can retrieve and set this value through the selected property.

      Available in iOS 2.0 and later.

    • focused

      UIControlStateFocused

      Focused state of a control. In focus-based navigation systems, a control enters this state when it receives the focus. A focused control changes its appearance to indicate that it has focus, and this appearance differs from the appearance of the control when it is highlighted or selected. Further interactions with the control can result in it also becoming highlighted or selected.

      Available in iOS 9.0 and later.

    • application

      UIControlStateApplication

      Additional control-state flags available for application use.

      Available in iOS 2.0 and later.

    • reserved

      UIControlStateReserved

      Control-state flags reserved for internal framework use.

      Available in iOS 2.0 and later.

    Discussion

    A control can have more than one state at a time. Controls can be configured differently based on their state. For example, a UIButton object can be configured to display one image when it is in its normal state and a different image when it is highlighted.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    enum UIControlContentVerticalAlignment : Int { case Center case Top case Bottom case Fill }

    Objective-C

    typedef enum UIControlContentVerticalAlignment : NSInteger { UIControlContentVerticalAlignmentCenter = 0, UIControlContentVerticalAlignmentTop = 1, UIControlContentVerticalAlignmentBottom = 2, UIControlContentVerticalAlignmentFill = 3, } UIControlContentVerticalAlignment;

    Constants

    • center

      UIControlContentVerticalAlignmentCenter

      Aligns the content vertically in the center of the control.

      Available in iOS 2.0 and later.

    • top

      UIControlContentVerticalAlignmentTop

      Aligns the content vertically at the top in the control (the default).

      Available in iOS 2.0 and later.

    • bottom

      UIControlContentVerticalAlignmentBottom

      Aligns the content vertically at the bottom in the control

      Available in iOS 2.0 and later.

    • fill

      UIControlContentVerticalAlignmentFill

      Aligns the content vertically to fill the content rectangle; images may be stretched.

      Available in iOS 2.0 and later.

    Discussion

    You use these constants as the value of the contentVerticalAlignment property.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    enum UIControlContentHorizontalAlignment : Int { case Center case Left case Right case Fill }

    Objective-C

    typedef enum UIControlContentHorizontalAlignment : NSInteger { UIControlContentHorizontalAlignmentCenter = 0, UIControlContentHorizontalAlignmentLeft = 1, UIControlContentHorizontalAlignmentRight = 2, UIControlContentHorizontalAlignmentFill = 3, } UIControlContentHorizontalAlignment;

    Constants

    • center

      UIControlContentHorizontalAlignmentCenter

      Aligns the content horizontally in the center of the control.

      Available in iOS 2.0 and later.

    • left

      UIControlContentHorizontalAlignmentLeft

      Aligns the content horizontally from the left of the control (the default).

      Available in iOS 2.0 and later.

    • right

      UIControlContentHorizontalAlignmentRight

      Aligns the content horizontally from the right of the control

      Available in iOS 2.0 and later.

    • fill

      UIControlContentHorizontalAlignmentFill

      Aligns the content horizontally to fill the content rectangles; text may wrap and images may be stretched.

      Available in iOS 2.0 and later.

    Discussion

    You use these constants as the value of the contentHorizontalAlignment property.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.