我有一个用程序创建的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磅。从按钮的左侧开始,同时垂直居中。到目前为止,图像显示为水平和垂直居中,但由于它似乎无法定义一个框架或类似的东西,我无法真正移动它。
有没有办法在它的父级(按钮)中移动图像?
谢谢。

最佳答案

也许您可以在一个imagePosition上使用NSButton属性?(documented here)。
它使用NSCellImagePositiondocumented here)类型的枚举,并允许您将图像设置为文本左侧、文本右侧、文本上方、文本下方等。
你仍然没有机会将像素完美地对齐,但是如果你能做到这一点,那么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 :)"

这给了我一个惊人的用户界面:
swift - 在NSButton中放置NSImage-LMLPHP
希望这对你有帮助。

关于swift - 在NSButton中放置NSImage,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43846508/

10-13 08:41