我有一个带有标准 NSWindow
的应用程序,其中包含一个 NSToolbar
以及一些用作首选项的 NSToolbarItem
。 NSToolbarItem
设置为可选择,并使用包含的"template"作为其名称的一部分以获得漂亮的系统渐变效果。
但是,当选择 NSToolbarItem
时,该选择不会更改文本的背景。
每个 NSToolbarItem
都标记为可选择,而我的 NSToolbarDelegate
仅实现这两个方法:
-(NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar{
NSMutableSet *toolbarSet = [NSMutableSet set];
for (NSDictionary *prefItem in _preferenceItems){
[toolbarSet addObject:[prefItem objectForKey:@"toolbarIdentifier"]];
}
return [toolbarSet allObjects];
}
-(NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar{
NSMutableSet *selectableSet = [NSMutableSet set];
for (NSDictionary *prefItem in _preferenceItems){
[selectableSet addObject:[prefItem objectForKey:@"toolbarIdentifier"]];
}
return [selectableSet allObjects];
}
这就是我所看到的;注意文本周围的灰色框:
与 Safari 的“偏好设置”窗口不同:
在 Interface Builder 中配置我的所有 NSToolbarItem
:
以及 NSToolbar
本身:
最佳答案
作为 NSToolbarDelegate/NSWindowController 设置的一部分,我设置了:
[[[self window] contentView] setWantsLayer:YES];
注释掉这一行解决了问题。
在上下文中:
-(void)awakeFromNib{
NSDictionary *zendeskItem = @{@"name" : @"Zendesk Accounts",
@"toolbarIdentifier" : @"zendesk"};
NSDictionary *toolItem = @{@"name" : @"Tools",
@"toolbarIdentifier" : @"tools"};
NSDictionary *supportItem = @{@"name" : @"Support",
@"toolbarIdentifier" : @"support"};
NSDictionary *healthItem = @{@"name" : @"Health",
@"toolbarIdentifier" : @"health"};
NSDictionary *aboutItem = @{@"name" : @"About",
@"toolbarIdentifier" : @"about"};
_preferenceItems = [NSArray arrayWithObjects:zendeskItem, toolItem, supportItem, healthItem, aboutItem, nil];
[[self window] setContentSize:[_zendeskView frame].size];
[[[self window] contentView] addSubview:_zendeskView];
//[[[self window] contentView] setWantsLayer:YES];
currentViewTag = 1;
}
关于objective-c - NSToolbarItem 选择不覆盖文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23699887/