blog icon indicating copy to clipboard operation
blog copied to clipboard

How to highlight selection of NSCollectionViewItem

Open onmyway133 opened this issue 5 years ago • 1 comments

Original answer https://stackoverflow.com/a/54793979/1418457


In your NSCollectionViewItem subclass, override isSelected and change background color of the layer. Test in macOS 10.14 and Swift 4.2

class Cell: NSCollectionViewItem {

  override func loadView() {    
    self.view = NSView()
    self.view.wantsLayer = true
  }

  override var isSelected: Bool {
    didSet {
      self.view.layer?.backgroundColor = isSelected ? NSColor.gray.cgColor : NSColor.clear.cgColor
    }
  }
}

onmyway133 avatar May 30 '19 17:05 onmyway133