class MyCell: UICollectionViewCell {
static let reuseIdentifier: String = "c"
}
class FullScreenCollVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var pageCollectionView: UICollectionView!
let colors: [UIColor] = [
.cyan, .yellow, .systemGreen, .systemBlue,
]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
setupPageCollectionView()
}
private func setupPageCollectionView() {
let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
self.view.backgroundColor = .red
pageCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
pageCollectionView.register(MyCell.self, forCellWithReuseIdentifier: MyCell.reuseIdentifier)
pageCollectionView.delegate = self
pageCollectionView.dataSource = self
pageCollectionView.decelerationRate = .fast
pageCollectionView.translatesAutoresizingMaskIntoConstraints = false
pageCollectionView.backgroundColor = .blue
self.view.addSubview(pageCollectionView)
NSLayoutConstraint.activate([
pageCollectionView.topAnchor.constraint(equalTo: self.view.topAnchor),
pageCollectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
pageCollectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
pageCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
print(indexPath.item, collectionView.bounds.size)
return collectionView.bounds.size
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let c = collectionView.dequeueReusableCell(withReuseIdentifier: MyCell.reuseIdentifier, for: indexPath) as! MyCell
c.contentView.backgroundColor = colors[indexPath.item % colors.count]
return c
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎