Simple question, but I can't find any answers... How I can change corner radius of NSCollectionViewItem
instance?
Asked
Active
Viewed 424 times
0

Artem Novichkov
- 2,356
- 2
- 24
- 34
-
item.layer.cornerRadius. import CoreGraphics. – GeneCode Jan 16 '17 at 09:19
-
@GeneCode here I need to set its property? In `viewDidLayout` of item? Of delegate method in controller that has `NSCollectionView` as subview? – Artem Novichkov Jan 16 '17 at 09:22
-
Should be in the collectionview subclass. (alternatively in the delegate) – GeneCode Jan 16 '17 at 09:28
-
@GeneCode I dont want to subclass `NSCollectionView`, in which delegate method I need to change corner radius? – Artem Novichkov Jan 16 '17 at 09:29
-
itemForRepresentedObjectAtIndexPath i think. – GeneCode Jan 16 '17 at 09:39
-
@GeneCode It doesn't work – Artem Novichkov Jan 16 '17 at 09:49
-
Did you set the item background color? – GeneCode Jan 16 '17 at 09:50
-
If you set the item bg color, then you need to set [item.layer setMaskToBounds:YES]; – GeneCode Jan 16 '17 at 09:52
1 Answers
1
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
}
}

Artem Novichkov
- 2,356
- 2
- 24
- 34