如何动态调整 NSMenu 的大小以适应其内容?

发布于 2024-11-19 13:09:17 字数 1776 浏览 3 评论 0 原文

我有一个像这样的标准 NSMenu

initial menu

此菜单是用以下代码创建的:

NSMenu *menu = [[NSMenu alloc] init];

NSMenuItem *addMenuItem = [[NSMenuItem alloc] init];
[addMenuItem setTitle:[NSString stringWithFormat:@"Upload"]];
[addMenuItem setHidden:NO];
[addMenuItem setRepresentedObject:contentType];
[addMenuItem setTarget:self];
[addMenuItem setAction:@selector(performSelectedDragOperation:)];
[addMenuItem setTag:operation];
[addMenuItem setEnabled:YES];

[menu addItem:addMenuItem];

NSMenuItem *alterMenuItem = [addMenuItem copy];
[alterMenuItem setTitle:@"Upload in Folder"];
[alterMenuItem setKeyEquivalentModifierMask:NSAlternateKeyMask];
[alterMenuItem setAlternate:YES];
[menu addItem:alterMenuItem];
[alterMenuItem release];

[addMenuItem release];

NSMenuItem *addMenuItem2 = [[NSMenuItem alloc] init];
[addMenuItem2 setTitle:[NSString stringWithFormat:@"EditNewAndUpload"]];
[addMenuItem2 setHidden:NO];
[addMenuItem2 setRepresentedObject:contentType];
[addMenuItem2 setTarget:self];
[addMenuItem2 setAction:@selector(performSelectedDragOperation:)];
[addMenuItem2 setTag:operation];
[addMenuItem2 setEnabled:YES];

[menu addItem:addMenuItem2];

NSMenuItem *alterMenuItem2 = [addMenuItem2 copy];
[alterMenuItem2 setTitle:@"EditNewAndUpload in Folder"];
[alterMenuItem2 setKeyEquivalentModifierMask:NSAlternateKeyMask];
[alterMenuItem2 setAlternate:YES];
[menu addItem:alterMenuItem2];
[alterMenuItem2 release];

[addMenuItem2 release];

如您所见,有一个“重复” " 菜单中每个选项的项目。当您在菜单可见的情况下按选项 (alt) 键时,会显示该项目的副本。

问题是菜单大小在显示备用菜单项时发生变化,因为该菜单的标题比原始菜单大,并且永远不会恢复到原始大小,从而产生以下内容:

在此处输入图像描述

知道如何修复它吗? 谢谢

I have a standard NSMenu like this

initial menu

This menu was created with this code:

NSMenu *menu = [[NSMenu alloc] init];

NSMenuItem *addMenuItem = [[NSMenuItem alloc] init];
[addMenuItem setTitle:[NSString stringWithFormat:@"Upload"]];
[addMenuItem setHidden:NO];
[addMenuItem setRepresentedObject:contentType];
[addMenuItem setTarget:self];
[addMenuItem setAction:@selector(performSelectedDragOperation:)];
[addMenuItem setTag:operation];
[addMenuItem setEnabled:YES];

[menu addItem:addMenuItem];

NSMenuItem *alterMenuItem = [addMenuItem copy];
[alterMenuItem setTitle:@"Upload in Folder"];
[alterMenuItem setKeyEquivalentModifierMask:NSAlternateKeyMask];
[alterMenuItem setAlternate:YES];
[menu addItem:alterMenuItem];
[alterMenuItem release];

[addMenuItem release];

NSMenuItem *addMenuItem2 = [[NSMenuItem alloc] init];
[addMenuItem2 setTitle:[NSString stringWithFormat:@"EditNewAndUpload"]];
[addMenuItem2 setHidden:NO];
[addMenuItem2 setRepresentedObject:contentType];
[addMenuItem2 setTarget:self];
[addMenuItem2 setAction:@selector(performSelectedDragOperation:)];
[addMenuItem2 setTag:operation];
[addMenuItem2 setEnabled:YES];

[menu addItem:addMenuItem2];

NSMenuItem *alterMenuItem2 = [addMenuItem2 copy];
[alterMenuItem2 setTitle:@"EditNewAndUpload in Folder"];
[alterMenuItem2 setKeyEquivalentModifierMask:NSAlternateKeyMask];
[alterMenuItem2 setAlternate:YES];
[menu addItem:alterMenuItem2];
[alterMenuItem2 release];

[addMenuItem2 release];

As you can see there is a "duplicate" item for each option in the menu. This copy of the item is shown when you push the option (alt) key wile the menu is visible.

The problem is that the menu size changes while it is showing the alternate menu items because the title of this menus is larger than the originals and it never reverts to the original size, producing this:

enter image description here

Any idea of how can I fix it?
Thanks

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

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

发布评论

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

评论(2

人心善变 2024-11-26 13:09:17

这是正常行为,您不必担心。如果您希望对其进行更改,请提交错误

This is normal behavior and you shouldn't worry about it. If you want it to change, file a bug.

豆芽 2024-11-26 13:09:17

您只需要更改 NSMenuItem 标题

[[appmenu itemAtIndex:0]setTitle:[NSString stringWithFormat:@"EditNewAndUpload \t \t \t \t \t"]];

You Just need change the NSMenuItem title

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