2

使用 obj c 代码的 Mac 书籍中提供了如何识别实际的 touchBar 硬件,以便我可以提供 touchbar 菜单选项。

4

3 回答 3

3

从苹果文档:

您的应用不需要也不需要 API 来知道是否有可用的 Touch Bar。无论您的应用程序是否在支持 Touch Bar 的机器上运行,您的应用程序的屏幕用户界面 (UI) 都会以相同的方式显示和运行。

要检查触摸栏是否可用(例如改进 UI/UX),您应该实现委托并设置一个 Bool ,如:

// Declare a class variable
@property (nonatomic, assign) BOOL isTouchBarAvailable;

@available(OSX 10.12.1, *)
// A bellow version can not be installed on a new MacBook. 
// Like an iPhone 7 can't have iOS9 installed.
- (NSTouchBar *)makeTouchBar
{
    // ... here the code to make the bar
    self.isTouchBarAvailable = YES
    return touchBar
}

来源:https ://developer.apple.com/reference/appkit/nstouchbar?language=objc

于 2017-02-13T07:57:47.053 回答
2

NSTouchBar 可以在软件和硬件中工作,正如 Xcode 中的模拟器所示。它不需要硬件来工作。但是,Xcode 8 发行说明告诉您如何测试操作系统是否支持任何类型的 Touch Bar 功能。

https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html

Xcode 8.1 支持包含它的 Mac 的 Touch Bar,并支持将 Touch Bar 功能添加到您的应用程序。(28859277) 在您的应用程序中使用触控栏功能之前,请确认该应用程序正在使用运行时检查支持触控栏的 macOS 版本上运行。

例如,以下 Objective-C 代码执行运行时检查以确保 NSTouchBar 可用:

NSClassFromString(@"NSTouchBar") != nil

在 Swift 代码中,对 macOS 10.12.1 进行可用性检查,并对 Touch Bar 类的可用性进行运行时检查。例如:

NSClassFromString("NSTouchBar") != nil

于 2017-02-22T15:48:03.513 回答
1

如果你想要一些控制,你可以使用isAutomaticCustomizeTouchBarMenuItemEnabled

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {

         // Here we just opt-in for allowing our instance of the
         // NSTouchBar class to be customized throughout the app.
        if #available(OSX 10.12.2, *) {
            NSApplication.shared().isAutomaticCustomizeTouchBarMenuItemEnabled = true
        }
    }
    /*
    Whether or not a menu item to customize the touch bar can be automatically 
    added to the main menu. It will only actually be added when hardware 
    or simulator is present. Defaults to NO. Setting this property to YES 
    is the recommended way to add the customization menu item.
    */
于 2017-02-13T08:03:27.367 回答