Class

UISlider

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

Overview

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.

Figure 1

Example UISlider object

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 valueChanged 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 isContinuous 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 addTarget(_:action:for:) 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.

Listing 1

Action methods for sliders

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

Debugging Sliders

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 UIControlState. 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.

Table 1

Core attributes

Attribute

Description

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 minimumValue and maximumValue properties.

Value Current

Represents the initial value of the slider. The value must be between the minimum and maximum values. Access this value at runtime with the value property.

Table 2 lists the attributes that control the appearance of a slider.

Table 2

Appearance attributes

Attribute

Description

Min Image

Specifies the image displayed at the leading end of the slider. If blank, no image is displayed. Access this value at runtime with the minimumValueImage property.

Max Image

Specifies the image displayed at the trailing end of the slider. If blank, no image is displayed. Access this value at runtime with the maximumValueImage property.

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 minimumTrackTintColor property.

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 maximumTrackTintColor property.

Thumb Tint

Controls the tint color of the slider’s thumb. Access this value at runtime with the thumbTintColor property.

Table 3 lists the attributes that configure the events associated with a slider.

Table 3

Event attributes

Attribute

Description

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 isContinuous property.

For information about the sliders’s inherited Interface Builder attributes, see UIControl and UIView.

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 minimumValueImage and maximumValueImage 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.

Figure 2

Slider minimum and maximum images

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 setMinimumTrackImage(_:for:), setMaximumTrackImage(_:for:), and setThumbImage(_:for:) methods. Set the capInsets property for the track images to facilitate horizontal stretching. To access the images used in the current control state, use the currentMinimumTrackImage, currentMaximumTrackImage, and currentThumbImage properties, as shown in Figure 3.

Figure 3

Slider track and thumb images

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 minimumTrackTintColor, maximumTrackTintColor, and thumbTintColor properties, as shown in Figure 4.

Figure 4

Slider tint colors

By default, the minimum track tint color defers to the tint color of the slider control.

Internationalization

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 minimumValueImageRect(forBounds:) or maximumValueImageRect(forBounds:) in a subclass of UISlider, be sure to take the user interface layout direction into account.

For more information, see Internationalization and Localization Guide.

Accessibility

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:

"Sound volume: 13 percent. Adjustable. Swipe up or down with one finger to adjust the value.”

For general information about making iOS controls accessible, see UISlider.

Symbols

Accessing the Slider’s Value

var value: Float

The slider’s current value.

func setValue(Float, animated: Bool)

Sets the slider’s current value, allowing you to animate the change visually.

Accessing the Slider’s Value Limits

var minimumValue: Float

The minimum value of the slider.

var maximumValue: Float

The maximum value of the slider.

Modifying the Slider’s Behavior

var isContinuous: Bool

A Boolean value indicating whether changes in the slider’s value generate continuous update events.

Changing the Slider’s Appearance

var minimumValueImage: UIImage?

The image representing the slider’s minimum value.

var maximumValueImage: UIImage?

The image representing the slider’s maximum value.

var minimumTrackTintColor: UIColor?

The color used to tint the default minimum track images.

var currentMinimumTrackImage: UIImage?

The minimum track image currently being used to render the slider.

func minimumTrackImage(for: UIControlState)

Returns the minimum track image associated with the specified control state.

func setMinimumTrackImage(UIImage?, for: UIControlState)

Assigns a minimum track image to the specified control states.

var maximumTrackTintColor: UIColor?

The color used to tint the default maximum track images.

var currentMaximumTrackImage: UIImage?

Contains the maximum track image currently being used to render the slider.

func maximumTrackImage(for: UIControlState)

Returns the maximum track image associated with the specified control state.

func setMaximumTrackImage(UIImage?, for: UIControlState)

Assigns a maximum track image to the specified control states.

var thumbTintColor: UIColor?

The color used to tint the default thumb images.

var currentThumbImage: UIImage?

The thumb image currently being used to render the slider.

func thumbImage(for: UIControlState)

Returns the thumb image associated with the specified control state.

func setThumbImage(UIImage?, for: UIControlState)

Assigns a thumb image to the specified control states.

Overrides for Subclasses

func maximumValueImageRect(forBounds: CGRect)

Returns the drawing rectangle for the maximum value image.

func minimumValueImageRect(forBounds: CGRect)

Returns the drawing rectangle for the minimum value image.

func trackRect(forBounds: CGRect)

Returns the drawing rectangle for the slider’s track.

func thumbRect(forBounds: CGRect, trackRect: CGRect, value: Float)

Returns the drawing rectangle for the slider’s thumb image.

Relationships

Inherits From

Conforms To