2012-02-25 28 views
0

我創建了一個自定義視圖類,因爲我想有一個狀態項目,你可以拖動項目。NSMenu內部NSStatusItem自定義NSView類不出現在狀態欄底部

這裏的視圖的定義:

@interface DragStatusView : NSImageView <NSMenuDelegate>{ 
    BOOL highlight; 
} 
@end 

在我ApplicationDelegate.m我實例化一個NSStatusItem,我DragStatusView的一個實例。我在DragStatusView上設置圖像,並將其菜單設置爲包含少量NSMenuItem的NSMenu實例。

- (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(!菜單出現在屏幕的頂部 - 啊)

我怎樣才能使菜單出現在狀態欄底部?

謝謝!

回答

1

終於搞定了。

代碼可以在這裏看到:

https://github.com/zbuc/imgurBar/blob/master/imgur/ApplicationDelegate.m https://github.com/zbuc/imgurBar/blob/master/imgur/StatusItemView.m

我不明白爲什麼這個代碼雖然作品,這讓我感到不舒服。它對邊界矩形做的事情略有不同,但我沒有看到現在做什麼。

+1

鏈接無法正常工作。你能否給我們另一個鏈接代碼。 – 2012-12-06 19:44:32

+0

感謝您的領導,更新鏈接 – ashgromnies 2012-12-07 23:33:23

1

你怎麼彈出菜單?你想要做的是這樣的:

- (void)mouseDown:(NSEvent *)event { 
    [statusItem popUpStatusItemMenu:[self menu]]; 
} 

當然,你的看法將需要一個參考狀態項,然後。

+0

我確實是這樣做的......編輯答案包括 – ashgromnies 2012-02-26 01:33:48

+1

奇怪......我不認爲我曾經有過這樣的事情。您的視圖的框架大小是否設置正確? – Wevah 2012-02-26 02:03:41

+0

無論我在這裏傳遞給NSRect:'DragStatusView * dragView = [[DragStatusView alloc] initWithFrame:NSMakeRect(0,30,30,30)];'似乎沒有任何區別 – ashgromnies 2012-02-26 03:34:55

相關問題