cocoaosx-snow-leopardnsmenuitemnsmenu

How do I adjust the size of an NSMenu to its content dynamically?


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


Solution

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