如何动态调整 NSMenu 的大小以适应其内容?
我有一个像这样的标准 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];
如您所见,有一个“重复” " 菜单中每个选项的项目。当您在菜单可见的情况下按选项 (alt) 键时,会显示该项目的副本。
问题是菜单大小在显示备用菜单项时发生变化,因为该菜单的标题比原始菜单大,并且永远不会恢复到原始大小,从而产生以下内容:
知道如何修复它吗? 谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正常行为,您不必担心。如果您希望对其进行更改,请提交错误。
This is normal behavior and you shouldn't worry about it. If you want it to change, file a bug.
您只需要更改 NSMenuItem 标题
You Just need change the NSMenuItem title