Search code examples
iosswiftuislider

How to change the circle color of Slider of SwiftUI?


I am trying to change the color of the circle of the SwiftUI Slider. But, unfortunately, I couldn't find a way. I am sharing the code I did till now. and also the screenshot of the view. I don't want to use UIKit Slider in my SwiftUI view. I appreciate any help and suggestions.

  Slider(
                value: $lightIntensity,
                in: 1...5,
                step: 2
            ) {
                /// Used for accessibility 
                Text(R.string.localized.settings_appliancedetail_button_interior_light())
            }
        onEditingChanged: { editing in
                isEditing = editing
            }
        .accentColor(R.color.primary.color)

Here is the screenshot of the view: enter image description here

So all I want is to change the white circle color to primary color.


Solution

  • You could try this approach using UISlider.appearance():

    struct ContentView: View {
        @Binding var lightIntensity: Float
    
        init(lightIntensity: Binding<Float>) {
            self._lightIntensity = lightIntensity
    
            UISlider.appearance().thumbTintColor = R.color.primary.color //<- here
        }
    }