How to properly disable a UISlider

I have a view controller where a UISwitch is used to enable resp. disable a UISlider:

class ViewController: UIViewController {
    ...
    @IBOutlet weak var slider: UISlider!
    
    @IBAction func doToggle(_ sender: UISwitch) {
        slider.isEnabled = sender.isOn
    }
    ...  
}

When doToggle is executed and slider.isEnabled is set to false the slider cannot be moved anymore which is expected behavior. Unexpectedly though, any blue color left to the knob remains unchanged and the slider looks as if it were still enabled.

I can only force the color change by adding an explicit call to setNeedsLayout().

     @IBAction func doToggle(_ sender: UISwitch) {
        slider.isEnabled = sender.isOn
        slider.setNeedsLayout()
    }

I am now wondering whether this a bug. Note that enabling / disabling a UIButton immediately also gives visual feedback that the button is enabled resp. disabled and there is no need to call setNeedsLayout().

Any insights are highly appreciated.

You can add:

          slider.isHighlighted = sender.isOn

That will give some dimming effect

Or change the color of the left part:

          slider.minimumTrackTintColor = sender.isOn ? .blue : .white

So, in your case:

  • slider.minimumTrackTintColor
  • slider.maximumTrackTintColor
  • slider.thumbTintColor

should let you achieve the exact UI you want.

You could also use the alpha of slider to dim it all much more.

.

I am still wondering why sliders appear to need extra treatment while other UI elements change their appearance immediately as a reaction to a change of isEnabled

Why is it so ? Hard to say. May be because it was considered difficult to decide which items should change to which appearance ? But I agree, that creates a confusing result.

.

 I am inclined to file a new entry in "Feedback Assistant"

You should, as a suggestion.

How to properly disable a UISlider
 
 
Q