2

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;
}
4

1 回答 1

7

我也遇到了这个。这很烦人。我的问题是我需要明确设置每个项目和包含组的 and minSizemaxSize也许这会起作用:

- (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。嗯。

于 2013-05-18T12:32:47.673 回答