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

问题描述 投票:3回答:2

我有这样的标准NSMenu

“初始菜单”

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

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

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

问题在于,菜单大小在显示替代菜单项时会发生变化,因为此菜单的标题大于原始菜单的标题,并且从不还原为原始大小,因此会产生以下问题:

“在此处输入图像描述”

关于如何解决它的任何想法?谢谢

cocoa osx-snow-leopard nsmenuitem nsmenu
2个回答
3
投票

这是正常行为,您不必为此担心。如果要更改它,请file a bug


0
投票

您只需要更改NSMenuItem标题

[[appmenu itemAtIndex:0]setTitle:[NSString stringWithFormat:@"EditNewAndUpload \t \t \t \t \t"]];
© www.soinside.com 2019 - 2024. All rights reserved.