2

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

J. Doe
  • 521
  • 4
  • 15

2 Answers2

1

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?

Michael
  • 6,451
  • 5
  • 31
  • 53
  • @J.Doe: you sure? Please change `circle.layer?` to `circle.layer!`. Also, do some NSLog-line. Do you see the log line in the console and the app does not crash? If you don't see the log line the code isn't executed at all. If it crashes, there is a problem with the layer. – Michael Aug 08 '18 at 19:52
  • no, it does not crash. I've printed `layer` and it prints without crashing – J. Doe Aug 08 '18 at 19:58
  • @J.Doe: you're right, it does not work. There does not seem to be an easy way to fix this. Please see my updated answer. – Michael Aug 08 '18 at 20:50
-1

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

Duncan C
  • 128,072
  • 22
  • 173
  • 272