在 NSButton 中定位 NSImage

Exo*_*cal 2 macos nsimage swift swift3

我有一个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
Run Code Online (Sandbox Code Playgroud)

我想要做的是获得 10pt 的图像。从按钮的左侧,同时垂直居中。到目前为止,图像显示为水平和垂直居中,但由于我似乎无法定义框架或类似的东西,因此我无法真正移动它。

有没有办法在其父级(按钮)内移动图像?

谢谢。

pbo*_*dsk 6

也许您可以imagePositionNSButton? (此处记录)。

它使用枚举类型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 :)"
Run Code Online (Sandbox Code Playgroud)

这给了我这个令人惊叹的用户界面:

带有图像的按钮 希望能帮到你。