迷失在 NSButton 类型和替代图像中
我想要一个带有图像和备用图像的 NSButton
。按下按钮时应显示备用图像,我还想通过代码显示备用图像,调用类似 [button setSelected:YES]
的内容。如果不手动使用 alternateImage
属性,这是否可能?
I’d like to have an NSButton
with an image and an alternate image. The alternate image should be displayed while the button is being pressed and I’d also like to display the alternate image from code, calling something like [button setSelected:YES]
. Is this possible without monkeing with the alternateImage
property by hand?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无需手动更改按钮的图像即可实现此操作:
在 Interface Builder(xib/nib 编辑器)中将 NSButton 的类型设置为“切换”,并且图像将自动更改为备用图像/标题。
This is possible without manually changing the button's image:
In Interface Builder (xib/nib Editor) set the Type of NSButton to "Toggle" and the image will automatically change to the alternate image/title.
您可以使用类型设置为
NSToggleButton
的NSButton
,然后使用image
和alternateImage
之间切换NSButton
的 code>NSOnState /NSOffState
状态。You can use a
NSButton
with type set toNSToggleButton
and then switch between theimage
and thealternateImage
using theNSOnState
/NSOffState
states of theNSButton
.最简单的方法是在两个图像之间切换:
The easiest way is to switch between the two images:
我只是在界面编辑器的属性检查器中构建了所有 10 种类型的按钮,没有添加任何代码。结果如下:
再次按下“切换”和“开关”将恢复到原始图像。
(如果您在按钮的属性检查器中将按钮类型更改为“单选”,它将恢复为“切换”。)
I just built all 10 types of buttons in the interface editor's attribute inspector with no added coding. Here are the results:
Toggle and Switch will revert to the original image when pressed again.
(If you change the button type to Radio in the button's Attribute Inspector, it reverts to Switch.)