UISwitch Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Declared in
UISwitch.h
Related sample code

Overview

You use the UISwitch class to create and manage the On/Off buttons used, for example, in the Settings app for options such as Airplane Mode and Bluetooth. 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 on or off.

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

For more information about appearance and behavior configuration, see “Switches”.

Tasks

Initializing the Switch Object

Setting the Off/On State

Customizing the Appearance of the Switch

Properties

offImage

The image displayed while the switch is in the off position.

@property(nonatomic, retain) UIImage *offImage
Discussion

In iOS 7, this property has no effect.

In iOS 6, 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.

Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

on

A Boolean value that determines the off/on state of the switch.

@property(nonatomic, getter=isOn) BOOL on
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.h

onImage

The image displayed when the switch is in the on position.

@property(nonatomic, retain) UIImage *onImage
Discussion

In iOS 7, this property has no effect.

In iOS 6, 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.

Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

onTintColor

The color used to tint the appearance of the switch when it is turned on.

@property(nonatomic, retain) UIColor *onTintColor
Availability
  • Available in iOS 5.0 and later.
Declared In
UISwitch.h

thumbTintColor

The color used to tint the appearance of the thumb.

@property(nonatomic, retain) UIColor *thumbTintColor
Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

tintColor

The color used to tint the outline of the switch when it is turned off.

@property(nonatomic, retain) UIColor *tintColor
Availability
  • Available in iOS 6.0 and later.
Declared In
UISwitch.h

Instance Methods

initWithFrame:

Returns an initialized switch object.

- (id)initWithFrame:(CGRect)frame
Parameters
frame

A rectangle defining the frame of the UISwitch object. 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.h

setOn:animated:

Set the state of the switch to On or Off, optionally animating the transition.

- (void)setOn:(BOOL)on animated:(BOOL)animated
Parameters
on

YES if the switch should be turned to the On position; NO if it should be turned to the Off position. If the switch is already in the designated position, nothing happens.

animated

YES to animate the “flipping” of the switch; otherwise NO.

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