Skip to content

UISlider ​

UISlider represents a slider: a horizontal track with a round thumb that the user drags to pick a number within a range. It is used for volume, brightness, playback position, and other places where you pick a value from a continuous range.

The range is set by minimumValue and maximumValue, 0 to 1 by default. Read and write the current value with value. When the user drags, the slider fires the change event; by default it fires many times continuously during the drag, and when isContinuous is set to false it fires only once, on release.

The slider's height is determined by the system; measured on iOS 27.2 it is 34 pt. The slider has no width of its own: your code must give it one, with frame or a width constraint. Without a width, it is 0 wide and not visible on screen.

The three numeric values are single-precision floats: writing 0.3 reads back 0.30000001192092896.

A slider is a control: enabling, state, and events come from UIControl.

Inheritance: EventTarget → NSObject → UIResponder → UIView → UIControl → UISlider

For common patterns, see the guide Building switches, sliders, and steppers.

Constructor ​

UISlider()

Creates a slider with a range of 0 to 1 and a value of 0.

Instance properties ​

Also inherits properties from UIControl and UIView. Inherited properties commonly used with sliders are isEnabled and frame.

Values:

PropertyDescription
valueThe current value
minimumValueThe lower bound of the range
maximumValueThe upper bound of the range
isContinuousWhether change fires continuously during a drag

Appearance:

PropertyDescription
sliderStyleWhether there is a thumb
minimumTrackTintColorThe color of the track to the left of the thumb
maximumTrackTintColorThe color of the track to the right of the thumb
thumbTintColorThe color of the thumb
minimumValueImageThe image outside the left end of the track
maximumValueImageThe image outside the right end of the track
currentThumbImage read-onlyThe thumb image used in the current state
currentMinimumTrackImage read-onlyThe left track image used in the current state
currentMaximumTrackImage read-onlyThe right track image used in the current state

Instance methods ​

Also inherits methods from UIControl and UIView.

MethodDescription
setValue()Changes the value, optionally animated

To replace the thumb and track with images, set them per state:

MethodDescription
setThumbImage()Sets the thumb image
setMinimumTrackImage()Sets the left track image
setMaximumTrackImage()Sets the right track image
thumbImageForState()The thumb image for a given state
minimumTrackImageForState()The left track image for a given state
maximumTrackImageForState()The right track image for a given state

Where each part is drawn within the slider:

MethodDescription
trackRectForBounds()The position of the track
thumbRectForBounds()The position of the thumb for a given value
minimumValueImageRectForBounds()The position of the left-end image
maximumValueImageRectForBounds()The position of the right-end image

Events ​

A slider's events are inherited from UIControl; listen with addEventListener(). The ones a slider uses are:

EventWhen it fires
changeThe user changed the value by dragging. When isContinuous is true it fires continuously during the drag; when false, once on release. Writing value from code doesn't fire it
touchdownA finger touches down on the slider
touchupinside, touchupoutsideThe finger lifts

Examples ​

Volume slider ​

Starting from a blank app, place a slider from 0 to 100 on screen, with a label above it showing the current value.

js
application.addEventListener('sceneconnect', (e) => {
  const win = new UIWindow(e.scene);
  const page = new UIViewController();
  page.view.backgroundColor = 'systemBackground';
  win.rootViewController = page;
  win.makeKeyAndVisible();

  const label = new UILabel();
  label.text = 'Volume 40';

  // Set the range first, then the value
  const slider = new UISlider();
  slider.maximumValue = 100;
  slider.value = 40;

  slider.addEventListener('change', () => {
    label.text = 'Volume ' + Math.round(slider.value);
  });

  // A vertical UIStackView stretches the slider to its own width
  const column = new UIStackView([label, slider]);
  column.axis = 'vertical';
  column.spacing = 12;
  column.translatesAutoresizingMaskIntoConstraints = false;
  page.view.addSubview(column);
  const safe = page.view.safeAreaLayoutGuide;
  NSLayoutConstraint.activateConstraints([
    column.topAnchor.constraintEqualToAnchor(safe.topAnchor, { constant: 24 }),
    column.leadingAnchor.constraintEqualToAnchor(safe.leadingAnchor, { constant: 16 }),
    column.trailingAnchor.constraintEqualToAnchor(safe.trailingAnchor, { constant: -16 }),
  ]);
});

Effect (not verified in the simulator)

  • "Volume 40" appears near the top of the screen, with a slider spanning the screen below it and the thumb toward the left.
  • Dragging the thumb changes the number in the label.
  • Dragging all the way to the right shows "Volume 100".

Corresponding iOS API ​

ObjCJS
[[UISlider alloc] init]new UISlider()
slider.value = 0.5fslider.value = 0.5
[slider setValue:0.5f animated:YES]slider.setValue(0.5, { animated: true })
slider.continuous = NOslider.isContinuous = false
slider.minimumTrackTintColor = UIColor.orangeColorslider.minimumTrackTintColor = 'orange'
[slider setThumbImage:image forState:UIControlStateNormal]slider.setThumbImage(image, { forState: ['normal'] })
[slider thumbImageForState:UIControlStateNormal]slider.thumbImageForState('normal')
[slider thumbRectForBounds:b trackRect:t value:v]slider.thumbRectForBounds(b, { trackRect: t, value: v })
slider.sliderStyle = UISliderStyleThumblessslider.sliderStyle = 'thumbless'

Comparison with the browser ​

BrowserNacre
<input type="range" min max step>new UISlider(), minimumValue, maximumValue. There is no step; see Snapping to fixed steps
input event (during the drag)change with isContinuous set to true
change event (on release)change with isContinuous set to false

See also ​