I\'m coming from the iOS development and I am wondering if there is a way to set up a NSCollectionView programmatically like a UICollectionView in
You need to embed NSCollectionView within NSScrollView. Code below is in Swift 4
Setup NSCollectionView
let layout = NSCollectionViewFlowLayout()
layout.minimumLineSpacing = 4
collectionView = NSCollectionView()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.collectionViewLayout = layout
collectionView.allowsMultipleSelection = false
collectionView.backgroundColors = [.clear]
collectionView.isSelectable = true
collectionView.register(
Cell.self,
forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell")
)
Setup NSScrollView
scrollView = NSScrollView()
scrollView.documentView = collectionView
view.addSubview(scrollView)
Here's a simple NSCollectionViewItem
class Cell: NSCollectionViewItem {
let label = NSTextField()
let imageView = NSImageView()
override func loadView() {
self.view = NSView()
self.view.wantsLayer = true
}
}