有没有一种方法可以将NSPopoverTouchBarItems数组添加到NSScrollView中?

目前,我的视图层次结构类似于以下列表。

  • NSTouchBar
  • NSCustomTouchBarItem
  • NSScrollView
  • NSStackView
  • NSButtons数组

  • 上面的层次结构输出以下屏幕截图。

    ios - NSScrollView(NSTouchBar)中的NSPopoverTouchBarItems-LMLPHP

    总之,最终目标是用NSPopoverTouchBarItems替换NSButtons数组。

    最佳答案

    我相信您需要使用NSScrubber来滚动或固定包括NSPopoverTouchBarItem的多个按钮的位置

    https://developer.apple.com/documentation/appkit/nsscrubber

    请查看此存储库,以获取更多信息和示例代码,这些信息可能会帮助您:

    https://github.com/loretoparisi/touchbar

    import Cocoa
    
    fileprivate extension NSTouchBar.CustomizationIdentifier {
        static let popoverBar = NSTouchBar.CustomizationIdentifier("com.TouchBarCatalog.popoverBar")
    }
    
    fileprivate extension NSTouchBarItem.Identifier {
        static let scrubberPopover = NSTouchBarItem.Identifier("com.TouchBarCatalog.TouchBarItem.scrubberPopover")
    }
    
    class PopoverScrubber: NSScrubber {
        var presentingItem: NSPopoverTouchBarItem?
    }
    
    class PopoverScrubberViewController: NSViewController {
        // MARK: NSTouchBar
    
        override func makeTouchBar() -> NSTouchBar? {
            let touchBar = NSTouchBar()
            touchBar.delegate = self
            touchBar.customizationIdentifier = .popoverBar
            touchBar.defaultItemIdentifiers = [.scrubberPopover]
            touchBar.customizationAllowedItemIdentifiers = [.scrubberPopover]
    
            return touchBar
        }
    }
    
    // MARK: NSTouchBarDelegate
    
    extension PopoverScrubberViewController: NSTouchBarDelegate {
        func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
            guard identifier == NSTouchBarItem.Identifier.scrubberPopover else { return nil }
    
            let popoverItem = NSPopoverTouchBarItem(identifier: identifier)
            popoverItem.collapsedRepresentationLabel = "Scrubber Popover"
            popoverItem.customizationLabel = "Scrubber Popover"
    
            let scrubber = PopoverScrubber()
            scrubber.register(NSScrubberTextItemView.self, forItemIdentifier: NSUserInterfaceItemIdentifier(rawValue: "TextScrubberItemIdentifier"))
            scrubber.mode = .free
            scrubber.selectionBackgroundStyle = .roundedBackground
            scrubber.delegate = self
            scrubber.dataSource = self
            scrubber.presentingItem = popoverItem
    
            popoverItem.collapsedRepresentation = scrubber
    
            popoverItem.popoverTouchBar = PopoverTouchBarSample(presentingItem: popoverItem)
    
            return popoverItem
        }
    }
    
    // MARK: NSScrubber Data Source and delegate
    
    extension PopoverScrubberViewController: NSScrubberDataSource, NSScrubberDelegate {
    
        func numberOfItems(for scrubber: NSScrubber) -> Int {
            return 20
        }
    
        func scrubber(_ scrubber: NSScrubber, viewForItemAt index: Int) -> NSScrubberItemView {
            let itemView = scrubber.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "TextScrubberItemIdentifier"), owner: nil) as! NSScrubberTextItemView
            itemView.textField.stringValue = String(index)
            return itemView
        }
    
        func scrubber(_ scrubber: NSScrubber, didSelectItemAt index: Int) {
            print("\(#function) at index \(index)")
    
            if let popoverScrubber = scrubber as? PopoverScrubber,
                let popoverItem = popoverScrubber.presentingItem {
                popoverItem.showPopover(nil)
            }
        }
    }
    

    关于ios - NSScrollView(NSTouchBar)中的NSPopoverTouchBarItems,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54610443/

    10-10 23:59