Я создал пользовательский класс представления, потому что хотел иметь элемент состояния, на который можно перетаскивать элементы.
Вот определение вида:
@interface DragStatusView : NSImageView <NSMenuDelegate>{
BOOL highlight;
}
@end
В моем ApplicationDelegate.mЯ создаю экземпляр NSStatusItem и экземпляр моего DragStatusView.Я установил изображение в DragStatusView, а также установил в его меню экземпляр NSMenu, содержащий несколько NSMenuItems.
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Install icon into the menu bar
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
NSImage *statusImage = [NSImage imageNamed:@"Status"];
[statusItem setImage:statusImage];
[menuItem setTitle:NSLocalizedString(@"Special Status", @"imgur menu item text")];
CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
NSRect itemRect = NSMakeRect(0.0, 0.0, NSSquareStatusItemLength, itemHeight);
DragStatusView* dragView = [[DragStatusView alloc] initWithFrame:itemRect];
[dragView retain];
[dragView setImage:statusImage];
[dragView setMenu:menu];
[statusItem setHighlightMode:YES];
[statusItem setView:dragView];
}
Вот метод в контроллере DragStatusView, который вызывает всплывающее меню:
- (void)mouseDown:(NSEvent *)event {
[[[NSApp delegate] statusItem] popUpStatusItemMenu:[self menu]]; // or another method that returns a menu
}
В основном это работает, однако меню появляется слишком высоко, когда вы нажимаете на элемент состояния.
Как это выглядит до нажатия: http://imgur.com/fpJcd,quS3c#1
Как выглядит посленажмите: http://imgur.com/fpJcd,quS3c#0 (меню отображается в верхней части экрана - ах!)
Как сделать так, чтобы меню отображалось в нижней части строки состояния?
Спасибо!