Objective c 我想知道状态栏图标在屏幕上的位置

Objective c 我想知道状态栏图标在屏幕上的位置,objective-c,xcode,macos,cocoa,Objective C,Xcode,Macos,Cocoa,我想知道状态栏图标在屏幕上的位置 -(void)awakeFromNib{ statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; NSBundle *bundle = [NSBundle mainBundle]; statusImage = [[NSImage alloc] initWithContentsOfFile:[

我想知道状态栏图标在屏幕上的位置

-(void)awakeFromNib{
    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
    NSBundle *bundle = [NSBundle mainBundle];
    statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"r" ofType:@"png"]];
    statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"rh" ofType:@"png"]]; 
    [statusItem setImage:statusImage];
    [statusItem setAlternateImage:statusHighlightImage];
    [statusItem setTitle:@"APP"];
    [statusItem setToolTip:@"You do not need this..."];
    [statusItem setHighlightMode:YES];

    NSRect rect = [[[statusItem view] window ] frame];

    NSLog(@"%f",rect.origin.y);

}
这样做并没有达到目的

NSRect rect = [[[statusItem view] window ] frame];

NSLog(@"%f",rect.origin.y);

您尚未为状态项设置自定义视图,因此调用
view
将返回nil

我猜您想知道位置的原因是当您单击状态项时

如果要为状态项实施操作,它可能如下所示:

- (IBAction)someAction:(id)sender
{
    NSWindow *window = [[[NSApplication sharedApplication] currentEvent] window];
    NSRect rect = [window frame];

    NSLog(@"%f",rect.origin.y);
}
[statusItem setAction:@selector(someAction:)];
然后将状态项的操作设置为:

- (IBAction)someAction:(id)sender
{
    NSWindow *window = [[[NSApplication sharedApplication] currentEvent] window];
    NSRect rect = [window frame];

    NSLog(@"%f",rect.origin.y);
}
[statusItem setAction:@selector(someAction:)];
然后,无论何时单击状态项,您都会在日志中看到类似的内容:

2012-04-17 20:40:24.344 test[337:403] 1578.000000
例如,可以使用该信息相对于状态项定位窗口(如Matt Gemmell的MAAttachedWindow)