簡體   English   中英

獲取NSStatusItem框架更改的通知?

[英]Get Notification of NSStatusItem frame change?

在使用帶有自定義視圖的NSStatusItem的應用程序中,如下所示:

在此輸入圖像描述

...如何在以下情況下收到通知:

  1. 由於全屏應用,狀態欄會被隱藏
  2. 狀態項移動位置,因為添加/刪除/調整了另一項?

當項目更改位置時,兩者都是將自定義視圖移動到正確位置所必需的。

有一個方法-[NSStatusItem setView:] 為狀態項設置自定義視圖時,此視圖將自動插入特殊狀態欄窗口。 您可以使用方法-[NSView window]訪問該窗口以觀察其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}}
}

每次狀態欄變為可見或隱藏以及移動圖標時,您都會收到通知。 這是您更新彈出式面板位置的機會。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM