【发布时间】:2014-04-22 23:47:10
【问题描述】:
我在应用程序的工具栏中有两个自定义的 NSToolbarItems。每个类都有一个 NSButton,我在其中设置按钮,然后将工具栏项的视图设置为按钮(例如停止按钮项):
@implementation RBSStopButtonToolbarItem
@synthesize button = _button;
-(id)initWithItemIdentifier:(NSString *)itemIdentifier
{
self = [super initWithItemIdentifier:itemIdentifier];
if(self)
{
// create button
_button = [[NSButton alloc] init];
// set the frame and bounds to be the same size
//[_button setFrameSize:NSMakeSize(64.0, 64.0)];
//[_button setBoundsSize:NSMakeSize(64.0, 64.0)];
// button will not have a visible border
[_button setBordered:NO];
// set the original and alternate images...names are "opposite"
[_button setImage:[NSImage imageNamed:@"StopButtonAlternateIcon"]];
[_button setAlternateImage:[NSImage imageNamed:@"StopButtonIcon"]];
// image position
[_button setImagePosition:NSImageOnly];
// set button type
[_button setButtonType:NSMomentaryChangeButton];
// button is transparent
[_button setTransparent:YES];
// set the toolbar item view to the button
[self setView:_button];
}
return self;
}
每个自定义 NSToolbarItem 我都有一个 IBOutlet:
// toolbar item for start button
IBOutlet RBSStartButtonToolbarItem *_startButtonToolbarItem;
// toolbar item for stop button
IBOutlet RBSStopButtonToolbarItem *_stopButtonToolbarItem;
但我没有在自定义视图工具栏项目中看到图像: 图像是 .icns 类型。我试图遵循的示例在这里: NSButton in NSToolbar item: click issue
有没有经验的人可以提供建议?
【问题讨论】:
-
尝试将
[self setView:_button];更改为[self.toolbarItem setView:_button]; -
@andrewbuilder "self" 指的是按钮所在的 NSToolbarItem 自定义类...
-
尝试使用 .png 文件。我记得在 Apple 文档中 .png 是首选的图像文件格式。可能是无法识别 .icns 文件类型。
-
我尝试了PNG,没有。不过还是谢谢...
标签: objective-c macos cocoa nstoolbar