Skip to content

Instantly share code, notes, and snippets.

@roman-wb
Created March 27, 2019 09:04
Show Gist options
  • Select an option

  • Save roman-wb/e940752314fc8c2e0db0b76b6a41ced0 to your computer and use it in GitHub Desktop.

Select an option

Save roman-wb/e940752314fc8c2e0db0b76b6a41ced0 to your computer and use it in GitHub Desktop.
Horizontal CollectionView with paging and center content inset (Variant 1: Multiple sections)
import UIKit
class CollectionCell: UICollectionViewCell {
static let reuseIdentifier = "CollectionCell"
static let inset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
var label: UILabel!
func setup(text: String) {
backgroundColor = UIColor.random()
label = UILabel(frame: .zero)
label.textAlignment = .center
label.text = text
addSubview(label)
label.snp.makeConstraints { (make) in
make.center.equalToSuperview()
make.width.height.equalToSuperview()
}
}
}
class CollectionViewController: UIViewController {
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: CollectionCell.reuseIdentifier)
collectionView.isPagingEnabled = true
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
collectionView.backgroundColor = UIColor.blue
collectionView.snp.makeConstraints { (make) in
make.center.equalToSuperview()
make.leading.trailing.equalToSuperview()
make.height.equalTo(350)
}
}
}
extension CollectionViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionCell.reuseIdentifier, for: indexPath) as! CollectionCell
cell.setup(text: "\(indexPath.section) -> \(indexPath.row)")
return cell
}
}
extension CollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = collectionView.frame.size
let inset = CollectionCell.inset
let width = size.width - inset.left - inset.right
let height = size.height - inset.top - inset.bottom
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return CollectionCell.inset
}
}
@irfanpule
Copy link

Where I can look extention snp ?

@nzrsky
Copy link

nzrsky commented Jul 20, 2020

Where I can look extention snp ?

SnapKit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment