Last active
December 12, 2020 04:54
-
-
Save denis-obukhov/121d89573720f0952f8e44c4935cb4f4 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
Collection view with UICollectionViewCompositionalLayout | |
resets section's content offset on vertical scroll | |
if visibleItemsInvalidationHandler is set. | |
Even if it's empty | |
*/ | |
import UIKit | |
class ViewController: UIViewController { | |
private(set) lazy var collectionView: UICollectionView = { | |
$0.contentInsetAdjustmentBehavior = .never | |
$0.showsVerticalScrollIndicator = false | |
$0.showsHorizontalScrollIndicator = false | |
$0.backgroundColor = .white | |
$0.register( | |
CardCell.self, | |
forCellWithReuseIdentifier: CardCell.reuseID | |
) | |
$0.dataSource = self | |
return $0 | |
}(UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)) | |
func createContentSection() -> NSCollectionLayoutSection { | |
let item = NSCollectionLayoutItem( | |
layoutSize: NSCollectionLayoutSize( | |
widthDimension: .fractionalWidth(1), | |
heightDimension: .fractionalHeight(1) | |
) | |
) | |
let group = NSCollectionLayoutGroup.horizontal( | |
layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.7), heightDimension: .fractionalHeight(0.5)), | |
subitem: item, | |
count: 1) | |
let section = NSCollectionLayoutSection(group: group) | |
section.contentInsets = NSDirectionalEdgeInsets(top: 50, leading: 50, bottom: 50, trailing: 50) | |
section.orthogonalScrollingBehavior = .groupPagingCentered | |
section.interGroupSpacing = 10 | |
// COMMENT THIS TO FIX CONTENT OFFSET RESET: | |
section.visibleItemsInvalidationHandler = { visibleItems, offset, environment in | |
//Empty or any actions like transforming scale while scrolling, etc | |
} | |
section.contentInsets = NSDirectionalEdgeInsets(top: 30, leading: 0, bottom: 0, trailing: 0) | |
return section | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
view.addSubview(collectionView) | |
collectionView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
collectionView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor), | |
collectionView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor), | |
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | |
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), | |
]) | |
} | |
private var collectionViewLayout: UICollectionViewLayout { | |
UICollectionViewCompositionalLayout(section: createContentSection()) | |
} | |
} | |
extension ViewController: UICollectionViewDataSource { | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
5 | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CardCell.reuseID, | |
for: indexPath) as! CardCell | |
cell.titleLabel.text = String(indexPath.row) | |
return cell | |
} | |
} | |
// MARK: - Cell | |
class CardCell: UICollectionViewCell { | |
static let reuseID = "CardCell" | |
let titleLabel: UILabel = { | |
$0.font = UIFont.systemFont(ofSize: 40) | |
$0.textAlignment = .center | |
$0.textColor = .white | |
$0.adjustsFontSizeToFitWidth = true | |
$0.minimumScaleFactor = 0.1 | |
return $0 | |
}(UILabel()) | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
commonInit() | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
commonInit() | |
} | |
private func commonInit() { | |
backgroundColor = UIColor.gray | |
layer.cornerRadius = 40 | |
layer.masksToBounds = true | |
contentView.addSubview(titleLabel) | |
titleLabel.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
titleLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor), | |
titleLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor), | |
titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor), | |
titleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), | |
]) | |
} | |
} |
Author
denis-obukhov
commented
Dec 12, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment