此处记录了NSToolbarItemGroup。我找不到比这更多的了!
我试着用它,看看它是什么样子。我的代码基本上是从文档中直接提取的。但是,我从来没有在我的工具栏中看到"组"。代码运行,项目被添加到工具栏,但它只是不可见?
有人成功地使用了这个吗?环境是10.8.3,使用最新的XCode。
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObject:@"GroupItem"];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObject:@"GroupItem"];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem *item1 = [[NSToolbarItem alloc] initWithItemIdentifier:@"Item1"];
NSToolbarItem *item2 = [[NSToolbarItem alloc] initWithItemIdentifier:@"Item2"];
[item1 setImage:[NSImage imageNamed:NSImageNameQuickLookTemplate]];
[item2 setImage:[NSImage imageNamed:NSImageNameQuickLookTemplate]];
[item1 setLabel:@"Prev"];
[item2 setLabel:@"Next"];
NSToolbarItemGroup *group = [[NSToolbarItemGroup alloc] initWithItemIdentifier:@"GroupItem"];
[group setSubitems:[NSArray arrayWithObjects:item1, item2, nil]];
[group setLabel:@"Navigate"];
return group;
}
我也遇到过这种情况。这是令人讨厌的。我的问题是,我需要显式地设置每个项目和包含组的minSize
和maxSize
。也许这将工作:
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem* retVal = nil;
if ([@"GroupItem" isEqual: itemIdentifier])
{
NSToolbarItem *item1 = [[NSToolbarItem alloc] initWithItemIdentifier:@"Item1"];
NSToolbarItem *item2 = [[NSToolbarItem alloc] initWithItemIdentifier:@"Item2"];
NSToolbarItemGroup *group = [[NSToolbarItemGroup alloc] initWithItemIdentifier:@"GroupItem"];
[group setSubitems:[NSArray arrayWithObjects:item1, item2, nil]];
item1.image = [NSImage imageNamed:NSImageNameQuickLookTemplate];
item1.minSize = item1.image.size;
item1.maxSize = item1.image.size;
item1.label = @"Prev";
item2.image = [NSImage imageNamed:NSImageNameQuickLookTemplate];
item2.minSize = item2.image.size;
item2.maxSize = item2.image.size;
item2.label = @"Next";
group.label = @"Navigate";
NSSize minSize = NSZeroSize;
NSSize maxSize = NSZeroSize;
for (NSToolbarItem* item in group.subitems)
{
minSize.width += item.minSize.width;
minSize.height = MAX(minSize.height, item.minSize.height);
maxSize.width += item.maxSize.width;
maxSize.height = MAX(maxSize.height, item.maxSize.height);
}
group.minSize = minSize;
group.maxSize = maxSize;
retVal = group;
}
return retVal;
}
这不是默认行为,这对我来说似乎很愚蠢,但这并没有阻止我浪费半个小时来弄清楚它。另外,要注意排序——如果在将子项放入组的子项集合之前设置了最小和最大大小,则组将将的大小重置为32x32。叽阿。