NSToolbarItem 可用性基于 OS X 版本

发布于 2024-11-24 02:37:00 字数 1447 浏览 1 评论 0原文

我有一个可在多个版本的 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;
}

所有其他工具栏项目都以正确的顺序显示,但是全屏项目位于最后,并且仍然存在。

提前致谢。

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?

Toolbar Customization

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下慵懒的猫 2024-12-01 02:37:00

除了 -toolbarAllowedItemIdentifiers: 之外,不要忘记 -toolbarDefaultItemIdentifiers:。请尝试如下操作:

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

请注意,如果您为工具栏设置了允许用户自定义,则可能需要删除应用程序首选项文件才能注意到工具栏设置中的更改。

Don't forget -toolbarDefaultItemIdentifiers: in addition to -toolbarAllowedItemIdentifiers:. Try something like the following:

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

Note that if you have allows user customization set for the toolbar, you may need to delete the apps pref file to notice a change in toolbar setup.

我家小可爱 2024-12-01 02:37:00

@NSGod 就快到了。虽然他的其余代码可以工作,但 IB 似乎正在覆盖我的代码并添加全屏按钮。我必须将工具栏项目移出工具栏,然后通过添加以下方法手动将 NSToolbarDelegate 指向它:(up, down,等是IBOutlet

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

@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 IBOutlets)

- (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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文