Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yfujiki/0547d95fe81fc2feae96a064f137fd74 to your computer and use it in GitHub Desktop.
Save yfujiki/0547d95fe81fc2feae96a064f137fd74 to your computer and use it in GitHub Desktop.
func setupBrandNamesSection() -> NSCollectionLayoutSection {
// 1. Creating section layout. Item -> Group -> Section
// Item
let item = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0)))
item.contentInsets = NSDirectionalEdgeInsets(top: 0.0,
leading: 8.0,
bottom: 0.0,
trailing: 8.0)
// Group
let group = NSCollectionLayoutGroup.vertical(
layoutSize: NSCollectionLayoutSize(widthDimension: .estimated(136),
heightDimension: .absolute(44)),
subitem: item,
count: 1)
// Section
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = NSDirectionalEdgeInsets(top: 16.0,
leading: 0.0,
bottom: 16.0,
trailing: 0.0)
// 2. Magic: Horizontal Scroll.
section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary
// 3. Creating header layout
section.boundarySupplementaryItems = [headerViewSupplementaryItem]
return section
}
private lazy var headerViewSupplementaryItem: NSCollectionLayoutBoundarySupplementaryItem = {
let headerViewItem = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .absolute(44)),
elementKind: UICollectionView.elementKindSectionHeader,
alignment: .top)
headerViewItem.pinToVisibleBounds = true
return headerViewItem
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment