在使用带有自定义视图的NsstatusItem的应用程序中,如下所示:
…如何在以下情况下收到通知:
>状态栏因全屏应用而被隐藏
>状态项移动位置,因为添加/删除/调整了其他项目的大小?
当项目更改位置时,两者都是将自定义视图移动到正确位置所必需的.
解决方法
有一个方法 – [NsstatusItem setView:].为状态项设置自定义视图时,此视图将自动插入特殊状态栏窗口.您可以使用方法 – [NSView窗口]访问该窗口以观察其NSWindowDidMoveNotification:
- (void)applicationDidFinishLaunching:(NSNotification *)notification { NsstatusItem *statusItem = [self newStatusItem]; NSView *statusItemView = [self newStatusItemView]; statusItem.view = statusItemView; NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; [dnc addobserver:self selector:@selector(statusBarDidMove:) name:NSWindowDidMoveNotification object:statusItemView.window]; } - (void)statusBarDidMove:(NSNotification *)note { NSWindow *window = note.object; NSLog(@"%@",NsstringFromrect(window.frame)); // i.e. {{1159,900},{24,22}} }
每次状态栏变为可见或隐藏以及移动图标时,您都会收到通知.这是您更新弹出式面板位置的机会.