我想知道屏幕上状态栏图标的位置
-(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);
答案 0 :(得分:4)
您尚未为状态项设置自定义视图,因此调用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:)];
然后每当您点击状态项时,您会在日志中看到一些错误信息:
2012-04-17 20:40:24.344 test[337:403] 1578.000000
您可以使用该信息,例如,相对于状态项定位窗口(如Matt Gemmell的MAAttachedWindow)。