简体   繁体   中英

Scaling does not work for NSSlider in MacOS

I have a NSSlider and I'm trying to scale it with the code:

let circle = NSSlider(frame: NSRect(x: 0.0, y: 333.0, width: 50.0, height: 50.0))
circle.layer?.setAffineTransform(CGAffineTransform(scaleX: 20.0, y: 20.0))
circle.wantsLayer = true
circle.sliderType = .circular

but this line does not work:

circle.layer?.setAffineTransform(CGAffineTransform(scaleX: 20.0, y: 20.0))

is there some other command in MacOS for scaling? I've tried to do as I was doing in iOS but it does not work I'm trying to make it x2 times bigger

It's not possible without hacking the NSSlider class significantly.

The NSSlider implementation keeps resetting the "affineTransform" of the layer to its original identity matrix (scale=1) in a non-key-value-observable way. The slider will change size for a moment, but then return to its original size.

(There is also a bug in your code, because you need to set wantsLayer=true before accessing the layer. But that's not enough.)

This question has an accepted answer that explains how to scale arbitrary NSViews. It works for labels, but it does not work for NSSliders.

What you can do: you can create your own slider class that duplicates the behaviour of NSSlider and which has a scale property. Or: you can maybe reverse engineer NSSlider to find out why it blocks attempts to scale it. Or: maybe you are happy with just adjusting the knobThickness of the slider?

The code CGAffineTransform(scaleX: 20.0, y: 20.0) would make the slider TWENTY times bigger! You should use CGAffineTransform(scaleX: 2.0, y: 2.0) to make it 2x biggger

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM