我正在用故事板为我的应用程序构建一个NSTouchBar。
我想用其他的东西替换ESC按钮。
和往常一样,没有医生告诉你怎么做。
我在网上搜索过,发现了一些模糊的信息,比如
通过使用escapeKeyReplacementItemIdentifier和NSTouchBarItem,您可以将"esc“的内容更改为其他内容,比如”已完成“或任何东西,甚至是图标。
但这太模糊了,无法理解。
有什么想法吗?
这就是我到目前为止所做的。
我在故事板上为NSTouchBar添加了一个按钮,并将其标识符更改为newESC。我以编程方式添加了这一行:
self.touchBar.escapeKeyReplacementItemIdentifier = @"newESC";当我运行应用程序时,ESC键现在是不可见的,但仍然占据它在栏上的空间。本应替换它的按钮出现在它旁边。所以那个酒吧
`ESC`, `NEW_ESC`, `BUTTON1`, `BUTTON2`, ...是现在
`ESC` (invisible), `NEW_ESC`, `BUTTON1`, `BUTTON2`, ...旧的ESC仍然占据着酒吧的空间。
发布于 2017-07-01 16:58:10
这是通过创建一个触摸条项来完成的,比如包含一个NSCustomTouchBarItem的NSButton,并将这个条目与它自己的标识符关联起来。
然后,使用另一个标识符执行通常的逻辑,但将先前创建的标识符添加为ESC替换。
Swift中的快速示例:
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItemIdentifier) -> NSTouchBarItem? {
switch identifier {
case NSTouchBarItemIdentifier.identifierForESCItem:
let item = NSCustomTouchBarItem(identifier: identifier)
let button = NSButton(title: "Button!", target: self, action: #selector(escTapped))
item.view = button
return item
case NSTouchBarItemIdentifier.yourUsualIdentifier:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSTextField(labelWithString: "Example")
touchBar.escapeKeyReplacementItemIdentifier = .identifierForESCItem
return item
default:
return nil
}
}
func escTapped() {
// do additional logic when user taps ESC (optional)
}我还建议为标识符创建一个扩展(类别),它避免使用字符串文字进行打字:
@available(OSX 10.12.2, *)
extension NSTouchBarItemIdentifier {
static let identifierForESCItem = NSTouchBarItemIdentifier("com.yourdomain.yourapp.touchBar.identifierForESCItem")
static let yourUsualIdentifier = NSTouchBarItemIdentifier("com.yourdomain.yourapp.touchBar.yourUsualIdentifier")
}https://stackoverflow.com/questions/44862210
复制相似问题