获取 NSStatusItem 框架更改的通知? [英] Get Notification of NSStatusItem frame change?
本文介绍了获取 NSStatusItem 框架更改的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
In an app that uses a NSStatusItem
with a custom view like this:
... how can you get notifications when:
- The status bar gets hidden because of a full screen app
- The status item moves position because another item is added/removed/resized?
Both are necessary to move the custom view to the right position when the item changes places.
解决方案
There is a method -[NSStatusItem setView:]
. When you set a custom view for your status item, this view is automatically inserted into a special status bar window. And you can access that window using a method -[NSView window]
to observe its 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}}
}
You will receive the notification every time the status bar becomes visible or hidden and when your icon is moved. This is your chance to update a location of your popup panel.
这篇关于获取 NSStatusItem 框架更改的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文