问题描述
首先,我知道已经回答了类似的问题,但是没有一个问题使我找到了所需的解决方案。
我正在尝试创建一个NSPopUpButton
,它具有图像和标题文本作为选择选项;我能做到的。问题是我似乎无法按照自己的意愿调整图像大小和填充(在每个方向上)。 (旁注:理想情况下,我会NSPopUpButton
加载默认情况下弹出窗口的第一张图片和标题。)
我尝试过:
func configurePopUpButton() {
let newMenuItem = NSMenuItem()
newMenuItem.title = myTitle
newMenuItem.image = myImage
newMenuItem.image?.size = NSSize(width: 20,height: 20)
myPopUpButton.menu?.addItem(newMenuItem)
}
但是这会调整myImage
的所有外观大小,并且由于图像确实出现在我项目的其他位置,所以这不好。而且,这不能解决问题的填充部分。
我也尝试过自定义NSPopUpButtonCell:
func configurePopUpButton() {
let cell = myPopUpButton.cell as? NSPopUpButtonCell
cell?.cellSize(forBounds: NSRect(x: 0,y: 0,width: 125,height: 25)) // doesn't change anything
cell?.imageScaling = .scaleProportionallyUpOrDown // doesn't change anything
cell?.backgroundColor = .systemRed // doesn't change anything
cell?.imageRect(forBounds: NSRect(x: 0,width: 20,height: 20)) // doesn't change anything
cell?.imagePosition = .imageRight // does change the image position
let newMenuItem = NSMenuItem()
newMenuItem.title = myTitle
newMenuItem.image = myImage
myPopUpButton.menu?.addItem(newMenuItem)
}
cell?.imagePosition = .imageRight
是对NSPopUpButtonCell
的唯一调整,似乎可以执行任何操作;但是,仅针对按钮,而不是弹出窗口中的NSMenuItem
。
我尝试过的另一种方法是创建自己的自定义NSMenuItem
视图:
func configurePopUpButton() {
let newMenuItem = NSMenuItem()
let frame = NSRect(x: 0,height: 30)
newMenuItem.view = MyCustomView(
image: myImage,title: myTitle,frameRect: frame
)
myPopUpButton.menu?.addItem(newMenuItem)
}
这看起来很棒!但是我现在无法选择任何选项,并且视图不会加载到NSPopUpButton
中,而不会加载到弹出窗口中。我也尝试了其他一些东西。这对我来说一团糟,我需要帮助!似乎应该很简单。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)