swiftui slider

Aug 20, 2021 swiftui

How Can we use Swiftui slider

 Use of Slider in SwiftUI

SWiftUI Slider : Earlier in Xcode we were using Slider in Storyboard or Xib.But As Apple Introduce SwiftUI Use of UIKit and Storyboard has been Depreciated in SwiftUI.   We can use the SwiftUI Slider in in the as Shown below.

let us start with the Slider implementation in SwiftUI.

Swiftui slider implementation

Initially create a Content View in Xcode as Shown in below code.

import SwiftUI

 

struct ContentView: View {

    var body: some View {

        Text("Hello, World!")

    }

}

 

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}

In the content View  create the variable which holds the value of the slider

   @State var sliderValue = 50.0

Add the Slider inside the body Specify the Range of the Slider

Slider(value: $slidervalue, in: 0...100)

We can change the increment the value by using the step

Slider(value: $slidervalue, in: 0...100, step: 2)

We can change the colour of the slider using the accent Color

  Slider(value: $slidervalue, in: 0...100, step: 2).accentColor(.orange)

We can change colour, padding and also the border of the Slider

struct Contentview2: View {

    @State var slidervalue = 50.0

    var body: some View {

        Slider(value: $slidervalue, in: 0...100, step: 2).accentColor(.orange)

        Text("SliderValue: (slidervalue)")

    }

}

 
Index