Created
March 27, 2019 09:04
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
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
Where I can look extention snp ?