SwiftUI forms: Slider

The Slider form control in SwiftUI lets us create a bar that the user can swipe left or right to decrease or increase its value.

We initialize a Slider by setting 3 parameters: value, in, step:
@State private var age: Double = 0

//…

Slide…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

The Slider form control in SwiftUI lets us create a bar that the user can swipe left or right to decrease or increase its value.

We initialize a Slider by setting 3 parameters: value, in, step:

@State private var age: Double = 0

//...

Slider(value: $age, in: 0...100, step: 1)

in limits the minimum and maximum values we can use.

step means we can step by a value of 1 at a time, in this case we can go from 0 to 1 to 2 etc. You could use 10, or 0.2, and so on.

Since Slider takes a Double value, by default we increment the decimals too.

Example:

struct ContentView: View {
    @State private var age: Double = 0
    
    var body: some View {
        Form {
            Slider(value: $age, in: 0...100, step: 1)
            Text("\(age)")
        }
    }
}

Notice how I added a Text view to show the value of age.

Since it’s a double, we have lots of decimals.

We could format that, but for this we’ll have another post.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-09-28T05:00:00+00:00) SwiftUI forms: Slider. Retrieved from https://www.scien.cx/2021/09/28/swiftui-forms-slider/

MLA
" » SwiftUI forms: Slider." flaviocopes.com | Sciencx - Tuesday September 28, 2021, https://www.scien.cx/2021/09/28/swiftui-forms-slider/
HARVARD
flaviocopes.com | Sciencx Tuesday September 28, 2021 » SwiftUI forms: Slider., viewed ,<https://www.scien.cx/2021/09/28/swiftui-forms-slider/>
VANCOUVER
flaviocopes.com | Sciencx - » SwiftUI forms: Slider. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/28/swiftui-forms-slider/
CHICAGO
" » SwiftUI forms: Slider." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/09/28/swiftui-forms-slider/
IEEE
" » SwiftUI forms: Slider." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/09/28/swiftui-forms-slider/. [Accessed: ]
rf:citation
» SwiftUI forms: Slider | flaviocopes.com | Sciencx | https://www.scien.cx/2021/09/28/swiftui-forms-slider/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.