Specifying increment Value
Specifying increment Value:
Using “step” define how to update the value stored in the binding value and display it
Code:
struct ContentView: View {
@State var Value: Double = 0
var body: some View {
VStack {
Slider(value: $Value, in: 0...100, step:10)
}.padding()
Text ("Slider value is \(Int(Value))")
}
}
Output:
Initial
On moving the slider
Making the Slider more Visual:
Add some text using HStacks to show the minimum and maximum values of the slider and display current
Code:
var body: some View {
VStack {
HStack{
Text("0")
Slider(value: $Value, in: 0...100, step:10)
Text("100")
}
}.padding()
Text ("Slider value is \(Int(Value))")
}
}
Output:
For decimal values upto 2 spaces after the decimal use (and add some font colour) we can change the code as follows :
Code:
var body: some View {
VStack {
HStack{
Text("0")
Slider(value: $Value, in: 0...100, step:10)
Text("100")
}
}.padding()
Text("Slider value: \(Value, specifier: "%.2f")")
.foregroundColor(.blue)
}
}
Output: