Swift 将NSImage定位到NSButton中

Swift 将NSImage定位到NSButton中,swift,macos,swift3,nsimage,Swift,Macos,Swift3,Nsimage,我有一个NSButton,是通过编程创建的。在所述按钮内,我有一个作为图标的NSImage: 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 我想做的是得到10磅的

我有一个
NSButton
,是通过编程创建的。在所述按钮内,我有一个作为图标的NSImage:

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
我想做的是得到10磅的图像。从按钮左侧开始,垂直居中。到目前为止,图像显示为水平居中和垂直居中,但由于它似乎无法定义帧或类似的内容,因此我无法真正移动它

是否有一种方法可以在其父对象(按钮)内移动图像


谢谢。

也许您可以使用
NSButton
上的
imagePosition
属性?()

它使用类型为
NSCellImagePosition
()的枚举,并允许您将图像设置为文本左侧、文本右侧、文本上方、文本下方等

你仍然没有机会将事物与像素完美地对齐,但如果你能接受这一点,那么
imagePosition
似乎是一条可行之路

以下是如何使用它:

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 :)"
这给了我这个惊人的用户界面:

希望这对你有帮助