Created
March 27, 2019 10:30
-
-
Save roman-wb/d76336ea753c1758bb59fa06ba11b731 to your computer and use it in GitHub Desktop.
Horizontal CollectionView with paging and center content inset (with shadow) (Variant 2: Cell with subview)
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" | |
| var card: UIView! | |
| var label: UILabel! | |
| var isInstalled = false | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| card = UIView(frame: .zero) | |
| addSubview(card) | |
| card.snp.makeConstraints { (make) in | |
| make.edges.equalToSuperview().inset(20) | |
| } | |
| label = UILabel(frame: .zero) | |
| label.textAlignment = .center | |
| card.addSubview(label) | |
| label.snp.makeConstraints { (make) in | |
| make.edges.equalToSuperview() | |
| } | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| func setup(text: String) { | |
| label.text = text | |
| } | |
| override func layoutSubviews() { | |
| super.layoutSubviews() | |
| if isInstalled { | |
| return | |
| } | |
| isInstalled = true | |
| card.dropShadow(roundedRect: card.bounds, | |
| cornerRadius: 30, | |
| fillColor: UIColor.white, | |
| shadowColor: UIColor.black, | |
| shadowOffset: CGSize(width: 0, height: 8), | |
| shadowOpacity: 0.1, | |
| shadowRadius: 15) | |
| } | |
| } | |
| class CollectionViewController: UIViewController { | |
| var collectionView: UICollectionView! | |
| override var preferredStatusBarStyle: UIStatusBarStyle { | |
| return .default | |
| } | |
| 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 | |
| collectionView.layer.masksToBounds = false | |
| collectionView.showsHorizontalScrollIndicator = false | |
| view.addSubview(collectionView) | |
| collectionView.backgroundColor = UIColor.clear | |
| 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 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 bounds = collectionView.bounds.size | |
| return CGSize(width: bounds.width, height: bounds.height) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment