在“仅文本”中单击时,NSToolbarItem (setView) 中的 NSButton强制模式为“图标和标签”

发布于 2024-09-12 12:11:43 字数 1210 浏览 2 评论 0原文

我正在尝试重新创建工具栏中的 Finder、Safari 和 Transmission 等漂亮的纹理按钮。首先,我首先在 IB 等中拖入“纹理按钮”。除非用户将工具栏设置为“仅文本”模式,否则一切正常。然后,当他单击该按钮时,工具栏将自行启用“图标和标签”。我已从工具栏中删除了所有代码和委托,以确保这不是代码问题。

然后,为了确保这一点,我创建了一个新项目(根本没有代码),我可以使用一个带有 NSToolbar 的干净 NSWindow 和一个带有 NSButton 的 NSToolbarItem 来重现该问题。

通过如下代码添加 NSButton:

- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
    return [NSArray arrayWithObject:@"myToolbarMenu"];
}

- (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
    return [self toolbarAllowedItemIdentifiers:toolbar];
}

- (NSToolbarItem*)toolbar:(NSToolbar*)toolbar
    itemForItemIdentifier:(NSString*)str
willBeInsertedIntoToolbar:(BOOL)flag
{
    if ([str isEqualToString:@"myToolbarItem"] == YES) {
        NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:str];
        [item setView:[[NSButton alloc] init]];
         [item setMinSize:NSMakeSize(50,50)];
        [item setMaxSize:NSMakeSize(50,50)];
        [item setLabel:@"Text"];
        return [item autorelease];  
    }
    return nil;
}

但这也具有相同的效果:当我在“仅文本模式”下按下带有 NSButton 的 NSToolbarItem 时,工具栏本身会强制其模式为“图标和文本”。

您是否知道我如何才能使其正常工作,或者是否有其他方法来创建 Safari 等漂亮的工具栏项目?

I am trying to recreate the nice textured buttons like Finder, Safari and Transmission have in their toolbar. First I started by just dragging in a "Texture button" in the IB and such. All works well except for when a user sets the toolbar to "Text only" mode. When he then clicks the button the toolbar will enable "Icon and Label" on it's own. I have remove alles code and delegates from the toolbar to make sure it is not a code issue.

Then, just to make sure, I created a new project (no code at all) and I can reproduce the issue with a clean NSWindow with a NSToolbar with one NSToolbarItem with a NSButton in it.

Adding the NSButtons via code like:

- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar {
    return [NSArray arrayWithObject:@"myToolbarMenu"];
}

- (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar {
    return [self toolbarAllowedItemIdentifiers:toolbar];
}

- (NSToolbarItem*)toolbar:(NSToolbar*)toolbar
    itemForItemIdentifier:(NSString*)str
willBeInsertedIntoToolbar:(BOOL)flag
{
    if ([str isEqualToString:@"myToolbarItem"] == YES) {
        NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:str];
        [item setView:[[NSButton alloc] init]];
         [item setMinSize:NSMakeSize(50,50)];
        [item setMaxSize:NSMakeSize(50,50)];
        [item setLabel:@"Text"];
        return [item autorelease];  
    }
    return nil;
}

But this also has the same effect: when I press a NSToolbarItem with a NSButton in it in "Text only mode" the toolbar itself forces it's mode to "Icon and Text".

Do you have any idea how I can make it work correctly or perhaps have an alternative to creating the nice looking toolbaritems like Safari etc have?

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

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

发布评论

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

评论(1

若有似无的小暗淡 2024-09-19 12:11:43

您需要向每个具有自定义视图的 NSToolbarItem 添加菜单表示。在分配 NSToolbarItem 的行下方添加以下内容:

NSMenuItem *menuRep = [[NSMenuItem alloc] initWithTitle:@"Text" action:@selector(targetMethod:) keyEquivalent:@""];
[menuRep setTarget:<target>];
[item setMenuFormRepresentation:menuRep];

只要目标有效,您的项目就应保留为纯文本按钮;否则他们将被禁用。请参阅 设置工具栏项目的表示

通常,您还需要在目标中实现 validateToolbarItem: ,但对于自定义视图项,您需要重写 validate: 来执行适当的操作。请参阅 验证工具栏项目

You need to add a menu representation to each NSToolbarItem that has a custom view. Below the line where you allocate the NSToolbarItem add this:

NSMenuItem *menuRep = [[NSMenuItem alloc] initWithTitle:@"Text" action:@selector(targetMethod:) keyEquivalent:@""];
[menuRep setTarget:<target>];
[item setMenuFormRepresentation:menuRep];

As long as the target is valid your items should stay as text-only buttons; otherwise they will be disabled. See Setting a Toolbar Item's Representation.

Normally you would also need to implement validateToolbarItem: in your target, but for custom view items you instead need to override validate: to do something appropriate. See Validating Toolbar Items.

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