首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向NSTouchBar控件条添加按钮

向NSTouchBar控件条添加按钮
EN

Stack Overflow用户
提问于 2017-12-27 16:39:47
回答 1查看 2.6K关注 0票数 3

我正在做一个宠物项目,在触摸栏上显示当前的加密货币价格。我是一名web开发人员,但不是Swift开发人员,所以进展很慢。

https://github.com/sharkattackhq/crypt

现在,基本功能已经就位,但数据仅在应用程序窗口处于活动状态时才会显示。我希望无论哪个应用程序处于活动状态,都能够查看数据,而我所能看到的唯一方法就是向触摸栏的Control able部分添加一个按钮。

如何将按钮添加到控制条?这方面没有公共API,但我可以看到其他没有苹果应用程序可以做到这一点。

EN

回答 1

Stack Overflow用户

发布于 2018-04-14 09:06:11

仅通过内网接口接入,需要接入/System/Library/PrivateFrameworks/DFRFoundation.framework

网桥:

代码语言:javascript
复制
// TouchBarPrivateApi-Bridging.h
#import "TouchBarPrivateApi.h"`

标题:

代码语言:javascript
复制
// TouchBarPrivateApi.
#import <AppKit/AppKit.h>`
extern void DFRElementSetControlStripPresenceForIdentifier(NSTouchBarItemIdentifier, BOOL);
    extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);

    @interface NSTouchBarItem (PrivateMethods)
    + (void)addSystemTrayItem:(NSTouchBarItem *)item;
    + (void)removeSystemTrayItem:(NSTouchBarItem *)item;
    @end


    @interface NSTouchBar (PrivateMethods)
    + (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar placement:(long long)placement systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
    + (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
    + (void)dismissSystemModalFunctionBar:(NSTouchBar *)touchBar;
    + (void)minimizeSystemModalFunctionBar:(NSTouchBar *)touchBar;
    @end

并使用它:

代码语言:javascript
复制
// AppDelegate applicationDidFinishLaunching
func applicationDidFinishLaunching(_ aNotification: Notification) {
    TouchBarController.shared.setupControlStripPresence()
}

// TouchBarController class TouchBarController: NSObject, NSTouchBarDelegate {

    static let shared = TouchBarController()

    let touchBar = NSTouchBar() func setupControlStripPresence() {
        DFRSystemModalShowsCloseBoxWhenFrontMost(false)
        let item = NSCustomTouchBarItem(identifier: .controlStripItem)
        item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
        NSTouchBarItem.addSystemTrayItem(item)
        DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
    }

更新10.14

代码语言:javascript
复制
+ (void)presentSystemModalTouchBar:(NSTouchBar *)touchBar placement:(long long)placement systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)presentSystemModalTouchBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSTouchBarItemIdentifier)identifier;
+ (void)dismissSystemModalTouchBar:(NSTouchBar *)touchBar;
+ (void)minimizeSystemModalTouchBar:(NSTouchBar *)touchBar;

以真实项目为例-- https://github.com/Toxblh/MTMR

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47988940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档