UISwitch Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | UISwitch.h |
Overview
You use the UISwitch class to create and manage the On/Off buttons you see, for example, in the preferences (Settings) for such services as Airplane Mode. These objects are known as switches.
The UISwitch class declares a property and a method to control its on/off state. As with UISlider, when the user manipulates the switch control (“flips” it) a UIControlEventValueChanged event is generated, which results in the control (if properly configured) sending an action message.
You can customize the appearance of the switch by changing the color used to tint the switch when it is in the on position.
For information about basic view behaviors, see View Programming Guide for iOS.
Tasks
Initializing the Switch Object
Setting the Off/On State
-
onproperty -
– setOn:animated:
Customizing the Appearance of the Switch
-
onTintColorproperty -
tintColorproperty -
thumbTintColorproperty -
onImageproperty -
offImageproperty
Properties
offImage
The image displayed while the switch is in the off position.
Discussion
This image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.
The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.
Availability
- Available in iOS 6.0 and later.
Declared In
UISwitch.hon
A Boolean value that determines the off/on state of the switch.
Discussion
This property allows you to retrieve and set (without animation) a value determining whether the UISwitch object is on or off.
Availability
- Available in iOS 2.0 and later.
Declared In
UISwitch.honImage
The image displayed when the switch is in the on position.
Discussion
This image represents the interior contents of the switch. The image you specify is composited with the switch’s rounded bezel and thumb to create the final appearance.
The size of this image must be less than or equal to 77 points wide and 27 points tall. If you specify larger images, the edges may be clipped.
Availability
- Available in iOS 6.0 and later.
Declared In
UISwitch.honTintColor
The color used to tint the appearance of the switch when it is turned on.
Availability
- Available in iOS 5.0 and later.
Declared In
UISwitch.hthumbTintColor
The color used to tint the appearance of the thumb.
Discussion
If the value of this property is nil, the tint color is derived from the value in the tintColor property.
Availability
- Available in iOS 6.0 and later.
Declared In
UISwitch.htintColor
The color used to tint the appearance when the switch is disabled.
Discussion
If you do not specify a color for the thumbTintColor property, this property is also used to tint the thumb of the switch.
Availability
- Available in iOS 6.0 and later.
Declared In
UISwitch.hInstance Methods
initWithFrame:
Returns an initialized switch object.
Parameters
- frame
A rectangle defining the frame of the
UISwitchobject. The size components of this rectangle are ignored.
Return Value
An initialized UISwitch object or nil if the object could not be initialized.
Discussion
UISwitch overrides initWithFrame: and enforces a size appropriate for the control.
Availability
- Available in iOS 2.0 and later.
Declared In
UISwitch.hsetOn:animated:
Set the state of the switch to On or Off, optionally animating the transition.
Parameters
- on
YESif the switch should be turned to the On position;NOif it should be turned to the Off position. If the switch is already in the designated position, nothing happens.- animated
YESto animate the “flipping” of the switch; otherwiseNO.
Discussion
Setting the switch to either position does not result in an action message being sent.
Availability
- Available in iOS 2.0 and later.
Declared In
UISwitch.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)