2
votes

I have an NSButton, created programatically. within said button, I have an NSImage that serves as an icon:

let button = NSButton(frame: NSRect(x: 10, y: (200 + (50 * id)), width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img

What I'm trying to do is get the image 10pt. from the left side of the button, while centred vertically. So far, the image shows up centered horizontally as well as vertically, but since it doesn't seem like I'm able to define a frame or something like that, I can't really move it.

Is there a way to move the image within it's parent (the button)?

Thanks.

1

1 Answers

6
votes

Maybe you could use the imagePosition property on a NSButton? (documented here).

It uses an enum of type NSCellImagePosition (documented here) and allows you to set the image to the left of the text, to the right of the text, above the text, below the text and so on.

You still won't have the opportunity to align things pixel perfect but if you can live with that, then imagePosition seems like the way to go.

Here is how to use it:

let button = NSButton(frame: NSRect(x: 10, y: 10, width: 100, height: 50))
button.action = #selector(self.switchToView)
let img = NSImage(named: "wob_icon")
button.image = img
button.imagePosition = .imageLeft
button.title = "Look left :)"

And that gives me this stunning UI:

button with image Hope that helps you.