How customise Slider blue line in SwiftUI?

后端 未结 4 1189
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 03:31

Like in UISlider

let slider = UISlider()

slider.minimumTrackTintColor = .red
4条回答
  •  自闭症患者
    2021-01-05 04:09

    If the bright white slider handle grates on your dark mode design, you can use .label color and .softLight to tell it to simmer down. It looks good only in grayscale, unless you can figure out the blend modes and hue rotation.

    The best looking result would be from an overlaid shaped with .blendMode(.sourceAtop)... but that blocks interaction, sadly.

    
    @Environment(\.colorScheme) var colorScheme
    
    var body: some View {
    
            let hackySliderBGColor: Color = colorScheme == .dark ? Color(.secondarySystemBackground) : Color(.systemBackground)
            let hackySliderAccentColor: Color = colorScheme == .dark ? Color(.label) : Color(.systemGray2)
            let hackySliderBlendMode: BlendMode = colorScheme == .dark ? .softLight : .multiply
    
    ...
    
        ZStack {
                Rectangle()
                .foregroundColor(hackySliderBGColor)
    
    // This second Rect prevents a white sliver if slider is at max value.
                 .overlay(Rectangle()
                      .foregroundColor(hackySliderBGColor)
                      .offset(x: 5)
                 )
    
                Slider(value: $pointsToScoreLimit, 
                       in: themin...themax, step: 5)
                .accentColor(hackySliderAccentColor)
                .blendMode(hackySliderBlendMode)
        }
    
    
    

    Example:

提交回复
热议问题