代码之家  ›  专栏  ›  技术社区  ›  Cemal K

MacOS在StatusBar上创建进度条

  •  1
  • Cemal K  · 技术社区  · 7 年前

    我正在寻找一种方法,将ProgressBar作为按钮放到Mac状态栏上。我想要这样的东西:

    Sample Image

    enter image description here

    我能做的是:

        let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.variableLength)
    
        [...]
    
        if let button = statusItem.button {
                    button.image = NSImage(named:NSImage.Name("JustSomeImage"))
                    button.title = "Hello"
                    button.action = #selector(togglePopover(_:))
    
                }
    

    但对于nsstatusitem,我只能使用按钮和菜单对象来操作nsstatusbar。所以我所能做的就是把一张图片和一些文字放在工具栏上。我已经搜索了很多寻找解决方案并阅读了文档,但不知何故我找不到一种方法来把进度条放到上面。有人知道我该怎么做吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Marc T.    7 年前

    因为nsbutton是nsview的子类,所以可以添加任何要添加的视图。下面您可以在目标C中找到一个小的代码片段,但将其转换为swift并不难。

    NSProgressIndicator *indicator = [[NSProgressIndicator alloc] initWithFrame:button.frame];
    [button addSubview:indicator];
    

    祝你好运和成功。