문제

NSToolbarItemGroup is documented here. I can't find much more than that about it!

I'm trying to use it, to see how it looks. My code is basically lifted directly from the documentation. However, I never see the 'group' in my toolbar. The code runs, and the item gets added to the toolbar, but it's just not visible?!

Has anyone had success with using this? Environment is 10.8.3 with latest 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;
}
도움이 되었습니까?

해결책

I ran into this too. It's annoying. My problem was that I needed to explicitly set the minSize and maxSize of each item and of the containing group. Maybe this will work:

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

Seems silly to me that this is not default behavior, but that didn't stop me from wasting half an hour figuring it out. Also, watch out for ordering -- if you set the subitems min and max size before putting them into the group's subitems collection, the group will reset the sizes for you to 32x32. Grrr.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top