2017-07-16 63 views
0

試圖讓按鈕,顯示觸摸條如何將視圖添加到NSGroupTouchBarItem

override func windowDidLoad() { 
    super.windowDidLoad() 
    let customViewItem = NSCustomTouchBarItem(identifier: NSTouchBarItemIdentifier(rawValue: "identifier")) 
    customViewItem.view = NSTextField(labelWithString: " ") 
    groupTouchBarItem.view?.addSubview(customViewItem) 
} 

上的一個可變的列表,但我得到Cannot invoke 'addSubview' with an argument list of type '(NSCustomTouchBarItem)'在底線上。

如何將按鈕添加到觸摸欄並隨意刪除它們?有沒有一個數組,我可以通過意見,或者我應該總是添加觸摸欄視圖的Subview?

回答

0

我建議檢查這guide on creating NSTouchBar and NSTouchBarItems,特別是「NSTouchBar對象」,「使用NSTouchBarItem對象」;以及NSGroupTouchBarItem

的文檔簡而言之,當您創建Touch Bar內容時,您可以通過將NSTouchBarItems組合到NSTouchBar中,而不是將它們的視圖組合到NSTouchBar中。 (你主要只是在NSCustomTouchBarItems上設置視圖時處理視圖)並且使用NSGroupTouchBarItem,你正在向groupTouchBar添加額外的項目。

有一些便利,例如:NSGroupTouchBarItem(identifier: .myGroupItem, items: [customViewItem]),但你也可以逐步添加到它:

groupItem.groupTouchBar.templateItems.insert(customViewItem) 
groupItem.groupTouchBar.defaultItemIdentifiers.append(customViewItem.identifier)