UISlider in Mac Catalyst updates UI too late

0

I am developing a Mac Catalyst app using UIKit. I have set the "Mac Catalyst Interface" of the target to "Optimize for Mac". All UISliders in my app update their UI "one interaction late": When I click on the slider track, the value of the slider changes accordingly, but the knob does not move to the appropriate position. Only when I click the second time, the knob moves to that position. But when I click on a different position on the track instead, the knob will move to the location corresponding to the previous click. What I tried:

  • Changing "isContinuous", it has no effect.
  • Listen for the "valueChanged" event and in the handler call "setNeedsLayout()" on the slider.
  • Listen for the "valueChanged" event and in the handler call "setNeedsDisplay()" on the slider.

When I change the target's "Mac Catalyst Interface" to "Scaled to match iPad", UISlider behaves as expected. But I need it to be "Optimize for Mac".

FWIW, here is my code to add a UISlider in a UIViewController, it is super basic stuff:

import UIKit

class ViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    let slider = UISlider()
    view.addSubview(slider)
    slider.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      slider.heightAnchor.constraint(equalToConstant: 50),
      slider.widthAnchor.constraint(equalToConstant: 300),
      slider.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      slider.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    ])
    slider.addTarget(self, action: #selector(onSlider(_:)), for: .valueChanged)
  }
  
  @objc func onSlider(_ slider: UISlider) {
//    slider.setNeedsLayout()
//    slider.setNeedsDisplay()
    print("on slider: \(slider.value)")
  }

}

Hey! we had the same problem. I don't know if this solution will be okay for you as it changes how UISlider works a little bit, but for us this worked

slider.preferredBehavioralStyle = .pad

From what I discovered .mac style disables sliding animation on click and this is why every next click make slider to be updated on previous click. .pad adds animation of sliding and probably because of it now positions itself correctly.

But we don't use images for slider, so I don't know if it's changed somehow, we use only line itself

UISlider in Mac Catalyst updates UI too late
 
 
Q