Я хотел бы сделать collectionView, который можно прокручивать по горизонтали и который имеет только один видимый элемент ячейки и один частично видимый.Клетки должны занимать всю высоту коллекции.
Я поиграл с разделом Inset of collectionView.Но не смог достичь своей цели.
Мой код:
import UIKit
class BooksController: UIViewController {
@IBOutlet weak var booksCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
layout.itemSize = CGSize(width: booksCollectionView.frame.width, height: booksCollectionView.frame.height)
layout.minimumInteritemSpacing = 10
layout.minimumLineSpacing = 10
layout.scrollDirection = .horizontal
booksCollectionView.collectionViewLayout = layout
booksCollectionView.register(UINib(nibName: "BookCell", bundle: .main), forCellWithReuseIdentifier: "BookCell")
booksCollectionView.dataSource = self
booksCollectionView.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension BooksController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCell", for: indexPath) as? BookCell {
return cell
}
return UICollectionViewCell()
}
}
extension BooksController: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
// func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
// return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 20)
// }
}
Цель (что-то похожее на коллекциюПросмотреть здесь)
Мой вывод