I'm trying to set up an NSCollectionView with a flow layout in vertical orientation. To quickly test the data source, I am returning static numbers and views from collectionView(_:numberOfItemsInSection:)
and collectionView(_:itemForRepresentedObjectAtIndexPath:)
respectively.
Here are the methods:
func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(collectionView: NSCollectionView,
itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
let item = NSCollectionViewItem()
let view = NSView()
item.view = view
view.wantsLayer = true
view.layer?.backgroundColor = CGColorCreateGenericGray(0, 0.5)
view.translatesAutoresizingMaskIntoConstraints = false
view.widthAnchor.constraintEqualToConstant(100).active = true
view.heightAnchor.constraintEqualToConstant(100).active = true
return item
}
Basically, I always return 2 NSCollectionViewItems that are all black with 50% opacity. When I run the app, it placed both views on top of each other.
Here's an exploded showing both views:
Each item is set to have a size of 50x50, and minimum spacing of 10 in each direction.
What am I missing? What makes these views be spaced apart? In the documentation, it says:
Normally, you specify the size of items and their spacing using the properties of this class. If you want to customize the values for your items, implement the methods of the NSCollectionViewDelegateFlowLayout protocol in the object assigned as the collection view’s delegate.
I'm not implementing this delegate, so shouldn't it work with the values I put in interface builder (50x50 and 10 spacing)?