Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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)
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