A visual control used to select a single value from a continuous range of values.
- iOS 2.0+
As you move the thumb of a slider, it passes its updated value to any actions attached to it. The appearance of sliders is configurable; you can tint the track and the thumb, and provide images to appear at the ends of the slider. You can add sliders to your interface programmatically or by using Interface Builder.
Figure 1 shows the terms used to describe the constituent parts of a
UISlider object in a left-to-right configuration.
The following steps are required to add a slider to your interface:
Specify the range of values the slider represents.
Optionally, configure the appearance of the slider with appropriate tint colors, and limit images.
Connect one or more action methods to the slider.
Set up Auto Layout rules to govern the size and position of the slider in your interface.
For information about basic view behaviors, see View Programming Guide for iOS.
Responding to User Interaction
Sliders use the Target-Action design pattern to notify your app when the user moves the slider. To be notified when the slider’s value changes, register your action method with the
value event. At runtime, the slider calls your method in response to the user changing the slider’s value.
By default, the slider sends value-changed events continuously as the user moves the slider’s thumb control. Setting the
is property to
false causes the slider to send an event only when the user releases the slider’s thumb control, setting the final value.
You connect a slider to your action method by using the
add method or by creating a connection in Interface Builder. The signature of an action method takes one of three forms, which are shown in Listing 1. Choose the form that provides the information that you need to respond to the value change in the slider.
When debugging issues with sliders, follow these tips to avoid common pitfalls:
Use either a custom tint color or a custom image, but not both. When customizing slider appearance with images or tint, use one option or the other, but not both. Conflicting settings for track and thumb appearance are resolved in favor of the most recently set value. For example, setting a new minimum track image for any state clears any custom tint color you may have provided for minimum track images. Similarly, setting the thumb tint color removes any custom thumb images associated with the slider.
The current value must be between the minimum and maximum values. If you try to programmatically set a slider’s current value to be below the minimum or above the maximum, it is set to the minimum or maximum instead. However, if you set the value beyond the range of the minimum or maximum in Interface Builder, the minimum or minimum values are updated instead.
Set custom images for all control states. If you use custom track and thumb images for your slider, remember to set an image for every possible
UIControl. Any control state that does not have a corresponding custom image assigned to it will display the standard image instead. If you set one custom image, be sure to set them all.
Interface Builder Attributes
Table 1 lists the core attributes that you configure for sliders in Interface Builder.
Value Minimum / Maximum
Specifies the values attached to the endpoints of the slider, the minimum representing the leading end of the slider and the maximum representing the trailing end. Access these values at runtime using the
Represents the initial value of the slider. The value must be between the minimum and maximum values. Access this value at runtime with the
Table 2 lists the attributes that control the appearance of a slider.
Specifies the image displayed at the leading end of the slider. If blank, no image is displayed. Access this value at runtime with the
Specifies the image displayed at the trailing end of the slider. If blank, no image is displayed. Access this value at runtime with the
Min Track Tint
Specifies the tint color of the track to the leading side of the slider’s thumb. The value defaults to the slider’s inherited tint color. Access this value at runtime with the
Max Track Tint
Specifies the tint color of the track to the trailing side of the slider’s thumb. Access this value at runtime with the
Controls the tint color of the slider’s thumb. Access this value at runtime with the
Table 3 lists the attributes that configure the events associated with a slider.
Events: Continuous Updates
Controls when attached actions are triggered: when checked, action events are called whenever the thumb is moved during user interaction. When not checked, attached actions are triggered only on completion of user interaction. Access this value at runtime with the
Customizing the Slider’s Appearance
Use Auto Layout to specify the position and width of a slider. The intrinsic height is determined by the intrinsic heights of the minimum and maximum images, if present. The width of the track automatically adjusts to accommodate the minimum and maximum images.
The most common way to customize the slider’s appearance is to provide custom minimum and maximum value images. These images sit at either end of the slider control and indicate which value that end of the slider represents. Set the values of the
maximum properties to appropriate
UIImage objects to display images at the ends of the slider. Figure 2 shows a slider configured with minimum and maximum images that imply volume adjustment.
You can also specify the images used to draw the thumb and the track. Provide images for each of the control states (normal, highlighted, and so on) with the
set methods. Set the
cap property for the track images to facilitate horizontal stretching. To access the images used in the current control state, use the
current properties, as shown in Figure 3.
If you want to change only the colors of the track and thumb, you don’t need to resort to custom images. You can set custom tint colors for both the track and the thumb of a slider, using the
thumb properties, as shown in Figure 4.
By default, the minimum track tint color defers to the tint color of the slider control.
Sliders have no special properties related to internationalization. However, if you use a slider with a label, make sure you provide localized strings for the label.
Sliders automatically adjust to the appropriate interface direction, ensuring that the minimum end of the slider is always at the leading end and the maximum end at the trailing end. If you override
maximum in a subclass of
UISlider, be sure to take the user interface layout direction into account.
For more information, see Internationalization and Localization Guide.
Sliders are accessible by default. The default accessibility traits for a slider are User Interaction Enabled and Adjustable.
When enabled on a device, VoiceOver speaks the accessibility label, value, traits, and hint to the user. VoiceOver speaks this information when a user swipes up and down (not left and right) over the slider. For example, using the Ringer and Alerts volume slider (Settings > Sounds > Ringer and Alerts), VoiceOver speaks the following:
For general information about making iOS controls accessible, see