I have an application that will be available across multiple versions of OS X. What's the best way to make an NSToolbarItem
only available to users in certain OS versions. When it is not available, it should be completely hidden, not just disabled.
To simplify, how do I remove a toolbar item from this (below) menu programmatically?
Edit: I tried to override toolbarAllowedItemIdentifiers:
in the delegate like so:
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
NSLog(@"Toolbar requesting allowed items.");
NSMutableArray *array = [NSMutableArray array];
[array addObject:@"TPUpToolbarItem"];
[array addObject:@"TPDownToolbarItem"];
[array addObject:@"TPResetToolbarItem"];
[array addObject:@"TPSpeedToolbarItem"];
[array addObject:@"TPGroupToolbarItem"];
[array addObject:@"TPBackgroundToolbarItem"];
[array addObject:NSToolbarShowFontsItemIdentifier];
if (floor(NSAppKitVersionNumber) <= 1038) {
NSLog(@"Below Lion, adding Fullscreen item.");
[array addObject:@"TPFSToolbarItem"];
}
[array addObject:@"TPFlipHToolbarItem"];
[array addObject:@"TPFlipVToolbarItem"];
[array addObject:NSToolbarFlexibleSpaceItemIdentifier];
[array addObject:NSToolbarSpaceItemIdentifier];
[array addObject:NSToolbarSeparatorItemIdentifier];
[array addObject:NSToolbarShowColorsItemIdentifier];
[array addObject:NSToolbarPrintItemIdentifier];
return array;
}
All the other toolbar items show in the correct order, however the Fullscreen item is last, and still there.
Thanks in advance.
@NSGod was almost there. While the rest of his code works, it appears IB is overriding my code and adding in the Fullscreen button anyway. I had to move the toolbar item out of the toolbar and then manually point the NSToolbarDelegate
to it by adding this method: (up
, down
, etc. are IBOutlet
s)
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
if ([itemIdentifier isEqualToString:@"TPUpToolbarItem"]) {
return up;
}
if ([itemIdentifier isEqualToString:@"TPDownToolbarItem"]) {
return down;
}
if ([itemIdentifier isEqualToString:@"TPResetToolbarItem"]) {
return reset;
}
if ([itemIdentifier isEqualToString:NSToolbarShowColorsItemIdentifier]) {
return colors;
}
if ([itemIdentifier isEqualToString:NSToolbarShowFontsItemIdentifier]) {
return fonts;
}
if ([itemIdentifier isEqualToString:NSToolbarPrintItemIdentifier]) {
return print;
}
if ([itemIdentifier isEqualToString:@"TPSpeedToolbarItem"]) {
return speed;
}
if ([itemIdentifier isEqualToString:@"TPBackgroundToolbarItem"]) {
return background;
}
if ([itemIdentifier isEqualToString:@"TPGroupToolbarItem"]) {
return group;
}
if ([itemIdentifier isEqualToString:NSToolbarFlexibleSpaceItemIdentifier]) {
return flex;
}
if ([itemIdentifier isEqualToString:NSToolbarSpaceItemIdentifier]) {
return space;
}
if ([itemIdentifier isEqualToString:@"TPFlipHToolbarItem"]) {
return flipH;
}
if ([itemIdentifier isEqualToString:@"TPFlipVToolbarItem"]) {
return flipV;
}
if ([itemIdentifier isEqualToString:NSToolbarSeparatorItemIdentifier]) {
return sep;
}
if ([itemIdentifier isEqualToString:@"TPFSToolbarItem"]) {
return fsItem;
}
}