2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

外観モードの変更を監視する(macOS / Objective-C)

2
Last updated at Posted at 2019-11-24

概要

  • アクアとダークモードでメニューバーアイコンを切り替える必要がでた。
  • (Apple)Supporting Dark Mode in Your Interfaceにあるように、ViewであればdrawRectなどで切り替えれば良さそう。
  • ただし今回はNSObject内で操作したいため再描画のトリガがない。よってNSNotificationで実装を行った。

参考

Code

[NSDistributedNotificationCenter.defaultCenter addObserver:self
                                                  selector:@selector(themeChanged:)
                                                      name:@"AppleInterfaceThemeChangedNotification"
                                                    object: nil];
-(void)themeChanged:(NSNotification *) notification {
    NSLog (@"%@", notification);
}
/**
 @brief 現在ダークモードかどうか
 */
- (BOOL)isDarkMode {
    if (@available(*, macOS 10.14)) {
        NSAppearance *appearance = NSApp.effectiveAppearance;
        NSLog(@"%@", NSAppearance.currentAppearance);
        return appearance.name == NSAppearanceNameDarkAqua;
    }
    return NO;
}
NSAppearance *appearance = NSApp.effectiveAppearance;

NSAppearanceのとり方に注意。

NSAppearance *appearance = NSAppearance.currentAppearance;
  • これだとNSAppearanceappearance.nameが初回起動時のテーマに固定されて値が帰ってくるので不適。
  • currentAppearance
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?