Latest Post

How to use closure button in swift How to use notificationcenter in swiftui with example Longest common subsequence program in swift

 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: 0100)

We can change the increment the value by using the step

Slider(value: $slidervalue, in: 0100, step: 2)

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

  Slider(value: $slidervalue, in: 0100, 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: 0100, step: 2).accentColor(.orange)

        Text(“SliderValue: (slidervalue))

    }

}

Leave a Reply

Your email address will not be published.