我有一个可在多个OS X版本上使用的应用程序。只有某些操作系统版本的用户可以使用NSToolbarItem
的最佳方法是什么。当它不可用时,它应该被完全隐藏,而不仅仅是禁用。
为了简化,如何以编程方式从此(下方)菜单中删除工具栏项?
修改:我试图在代理中覆盖toolbarAllowedItemIdentifiers:
,如下所示:
- (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;
}
所有其他工具栏项目都以正确的顺序显示,但全屏项目是最后一项,并且仍在那里。
提前致谢。
答案 0 :(得分:2)
除了-toolbarDefaultItemIdentifiers:
之外,别忘了-toolbarAllowedItemIdentifiers:
。尝试以下内容:
enum {
MDUndeterminedVersion = 0,
MDTiger = 0x1040,
MDLeopard = 0x1050,
MDSnowLeopard = 0x1060,
MDLion = 0x1070,
MDMountainLion = 0x1080,
MDMavericks = 0x1090,
MDUnknownVersion = 0x1100 // ??
};
static SInt32 MDSystemVersion = MDUndeterminedVersion;
+ (void)initialize {
if (MDSystemVersion == MDUndeterminedVersion) {
SInt32 MDFullSystemVersion = 0;
Gestalt(gestaltSystemVersion, &MDFullSystemVersion);
MDSystemVersion = MDFullSystemVersion & 0xfffffff0;
}
}
- (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 (MDSystemVersion < MDLion) {
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;
}
-
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
NSLog(@"Toolbar requesting default 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 (MDSystemVersion < MDLion) {
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;
}
请注意,如果您允许为工具栏设置用户自定义,则可能需要删除apps pref文件以注意工具栏设置中的更改。
答案 1 :(得分:1)
使用Gestalt。
答案 2 :(得分:0)
@NSGod几乎就在那里。当他的其余代码工作时,看起来IB正在覆盖我的代码并且无论如何都要添加全屏按钮。我必须将工具栏项移出工具栏,然后通过添加此方法手动将NSToolbarDelegate
指向它:(up
,down
等等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;
}
}