Xcode 如何在SwiftUI MacOS应用程序中使用NSTouchBar?

Xcode 如何在SwiftUI MacOS应用程序中使用NSTouchBar?,xcode,macos,swiftui,nstouchbar,Xcode,Macos,Swiftui,Nstouchbar,我正在尝试在SwiftUI中为我的MacOS应用程序添加触摸栏支持 我发现我必须打电话给 NSApp.isAutomaticCustomizeTouchBarMenuItemEnabled = true 。。在我的AppDelegate中。但是现在创建触摸栏的最佳方法是什么?我可以将其添加到Mac应用程序菜单的情节提要中,还是需要以编程方式创建它 当我遵循苹果的文档时,我达到了这一点: 通过将一个触摸栏添加到情节提要中的视图控制器,或通过重写NSResponder的makeTouchBar

我正在尝试在SwiftUI中为我的MacOS应用程序添加触摸栏支持

我发现我必须打电话给

  NSApp.isAutomaticCustomizeTouchBarMenuItemEnabled = true
。。在我的AppDelegate中。但是现在创建
触摸栏的最佳方法是什么?我可以将其添加到Mac应用程序菜单的情节提要中,还是需要以编程方式创建它

当我遵循苹果的文档时,我达到了这一点:

通过将一个触摸栏添加到情节提要中的视图控制器,或通过重写NSResponder的makeTouchBar()函数以编程方式创建一个触摸栏,可以创建一个触摸栏

是否可以创建自己的NSViewController,然后将其嵌入NSNiewController representable中


也许任何人都有一个很好的建议,告诉我如何在SwiftUI中支持TouchBar。

最简单的方法就是在逻辑允许的情况下为视图使用特定的标识符,如下面的示例所示

struct ContentView: View {
  var body: some View {
     SomeView()      // << your view here 
        .touchBar {
            Button("Do") {
               // .. Do something here
            }
        }
  }
}

非常感谢。我没找到一个多么糟糕的人。touchBar()。我必须在我的系统首选项中启用“使用键盘导航在控件之间移动焦点”,然后我可以在虚拟触摸栏上看到它。谢谢我必须在.touchBar()之前添加.focusable(),才能让它工作。可能是个虫子。
extension View {

    /// Sets the `TouchBar` to be shown in the Touch Bar when applicable.
    @available(iOS, unavailable)
    @available(tvOS, unavailable)
    @available(watchOS, unavailable)
    public func touchBar<Content>(_ touchBar: TouchBar<Content>) -> some View where Content : View

    /// Sets the `TouchBar` content to be shown in the Touch Bar when applicable.
    @available(iOS, unavailable)
    @available(tvOS, unavailable)
    @available(watchOS, unavailable)
    public func touchBar<Content>(@ViewBuilder content: () -> Content) -> some View where Content : View
}