macOS Develop

macOS.创建状态栏 App

2017-11-13  本文已影响3人  SMThree

macOS 上有很多应用是特别简单,只在状态栏上有图标,点击之后会弹出一个简易的 UI,在 UI 上可以进行一系列操作。比如番茄土豆,还有 Alfred 等。

这篇文章主要就是介绍如何创建这些应用。

系统环境 : macOS 10.13 & IDE : Xcode 9.0。

创建好应用之后,需要做一下一些配置:

  1. 在 info.plist 文件里增加一项设置: Application is agent (UIElement) 为 YES。
  2. 将 main.storyboard 里面除 Application scene 之外的 scene 全部删除。

这时候运行工程的话,应该是什么都看不到。

继续配置,在 AppDelegate.m 里 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 方法进行写如下代码:

    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:25];
    statusItem.image = [NSImage imageNamed:NSImageNameUserGroup];
    statusItem.button.target = self;
    statusItem.button.action = @selector(hello:);
增加对应方法
- (void)hello:(id)sender {
    NSLog(@"hello world");
}

这时候运行工程,状态栏出现应用图标:两个人的 UserGroup 图片,然后点击该图标的时候会输出"hello world"。

至于怎么点击应用弹出窗口,也很简单,具体思路是这样的: AppDelegate -> WindowController(窗口控制单例) -> Window(.contentView) -> ViewController ->View。

这个命题的关键的是设置窗口的位置,为了良好的用户体验,窗口应该不会太大导致超出屏幕。

以上。

上一篇 下一篇

猜你喜欢

热点阅读