如何在viewcontroller中显示触摸栏

bew*_*ils 6 macos swift nstouchbar

我想开发一个带触摸条的应用程序.我在互联网上搜索并看到了一些教程.他们都使用windowcontroller进行演示,只需makeTouchBar()在故事板中覆盖或拖动触摸栏,然后就会有一个触摸栏.但是我想在viewcontroller中使touchbar变得不同.然后我将触摸条拖入故事板中的viewcontroller,并在viewcontroller中绑定触摸栏.

在此输入图像描述

bind(#keyPath(touchBar), to: self, withKeyPath: #keyPath(touchBar), options: nil)

但是当我运行项目时,我找不到我的触摸栏,然后我打印touchbar.isVisible,我发现该值为false.

图像描述在这里

那么如何在viewcontroller中显示触摸栏.谢谢!

Loy*_*oys 5

你可以在ViewController中试试这个,它对我有用:

override func viewDidAppear() {
        super.viewDidAppear()
        self.view.window?.unbind(#keyPath(touchBar)) // unbind first
        self.view.window?.bind(#keyPath(touchBar), to: self, withKeyPath: #keyPath(touchBar), options: nil)
}
Run Code Online (Sandbox Code Playgroud)