
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - Slider
Slider is also a UI control in SwiftUI which allows the user to select a value from the continuous range by dragging the given symbol in the sliding bar. The end points of the slider represent the maximum and minimum values. It is commonly used in adjusting the sound, brightness, etc. in the app.
Creating Slider in SwiftUI
In SwiftUI, we can create slider control using the pre-defined Slider view. Just like other UI controls it also requires a biding value that represents the current state of the slider and a range in between the user selects the value.
Syntax
Following is the basic slider syntax −
Slider( value: Binding , in bounds: ClosedRange , step: V.Stride = 1, onEditingChanged: @escaping (Bool) -> Void = { _ in } ) where V : BinaryFloatingPoint, V.Stride : BinaryFloatingPoint
Parameters
Following parameters are used by this view −
value: Represent the selected value in the given range.
bounds: Represent the valid range.
step: Represent the distance between each value.
onEditingChanged: Represent a closure called when the editing begins or ends.
Example 1
The following SwiftUI program creats a simple slider.
import SwiftUI struct ContentView: View { @State private var input : Float = 0.0 var body: some View { VStack{ Text("Adjust Brightness") Slider(value: $input, in: 0...20) } } } #Preview { ContentView() }
Output

Example 2
The following SwiftUI program creates multiple sliders with steps. Here the first slider is set to step = 2 which means the slider will move by 2 units whereas the second slider is set to step = 1 which means the slider will move by 1 unit.
import SwiftUI struct ContentView: View { @State private var input1 : Float = 0.0 @State private var input2 : Float = 0.0 var body: some View { VStack{ Text("Product Quantity") Slider(value: $input1, in: 1...20, step: 2) Text("Product Amount") Slider(value: $input2, in: 1...15, step: 1) } } } #Preview { ContentView() }
Output

Example 3
The following SwiftUI program is used to customize the slider view. Here we display the minimum and maximum values at the end points using minimumValueLabel and maximumValueLabel. Also, change the color of the slider to green color using the tint() modifier.
import SwiftUI struct ContentView: View { @State private var input : Float = 0.0 var body: some View { VStack{ Text("Product Quantity") Slider(value: $input, in: 1...20, step: 2){ Text("Slider") }minimumValueLabel: { Text("0").font(.title2) }maximumValueLabel: { Text("20").font(.title2) }.tint(.green) } } } #Preview { ContentView() }
Output
