objective-c - NSToolbarItem 选择不覆盖文本

标签 objective-c macos cocoa interface-builder nstoolbar

我有一个带有标准 NSWindow 的应用程序,其中包含一个 NSToolbar 以及一些用作首选项的 NSToolbarItemNSToolbarItem 设置为可选择,并使用包含的"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];
}

这就是我所看到的;注意文本周围的灰色框:

behavior

与 Safari 的“偏好设置”窗口不同:

safari

在 Interface Builder 中配置我的所有 NSToolbarItem:

ib

以及 NSToolbar 本身:

toolbar

最佳答案

作为 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/

相关文章:

iphone - 如何重命名 objective-c 中捕获的图像?

ios - 如何在 iOS 中使用两个不同的表

ios - UIButton 在 iOS 7 中无法正常工作

c - 如何使用leaks命令行工具查找内存泄漏?

ios - 在 UIViews 层动画后,UIView 上的 UIPanGestureRecognizer 行为异常

macos - Mac 开发入门的最佳方式

amazon-web-services - 如何从命令行访问 Amazon S3 上的文件?

macos - NSUserDefaults 是否与包标识符相关联?

Objective-c 获取进程列表、它们的路径和参数

objective-c - 我是否需要释放 sortedArrayUsingDescriptors 返回的 NSArray?