swiftmacosnscollectionviewitem

Rounded corners of NSCollectionViewItem


Simple question, but I can't find any answers... How I can change corner radius of NSCollectionViewItem instance?


Solution

  • view of NSCollectionViewItem doesn't have layer by default. You need to set wantsLayer to true, for example:

    import Cocoa
    
    class TestCellItem: NSCollectionViewItem {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.wantsLayer = true
            view.layer?.backgroundColor = NSColor.red.cgColor
        }
    
        override func viewDidLayout() {
            super.viewDidLayout()
            view.layer?.cornerRadius = 20
        }
    }