我对此代码有疑问。我需要将水平滚动集合视图从屏幕的边缘扩展到边缘,以使主单元格显示在屏幕中央,并显示前一个和下一个单元格,但是我不知道该怎么做,我在网上找不到任何东西。 This is the problem
class ViewController: UIViewController {
view.addSubview(collectionView)
collectionView.backgroundColor = .clear
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 80).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -70).isActive = true
collectionView.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView.showsVerticalScrollIndicator = false
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CustomCell.self.forCellWithReuseIdentifier: "cellId")
}
}
extension ViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width - 10, height: collectionView.frame.width * 1.6)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return CategoriesViewController.defaultQuotes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CustomCell
cell.data = CategoriesViewController.defaultQuotes[indexPath.row]
return cell
}
}
class CustomCell: UICollectionViewCell {
var data:CustomData? {
didSet {
guard let data = data else { return }
//bg.image = data.image
text.text = data.title
authorLabel.text = "- " + data.author
}
}
fileprivate let text: UILabel = {
let quote = UILabel()
quote.textAlignment = .center
quote.translatesAutoresizingMaskIntoConstraints = false
return quote
}()
fileprivate let container: UIImageView = {
let view = UIImageView()
view.image = BackgroundImages.images[0]
return view
}()
func setUpViews() {
contentView.addSubview(container)
container.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
container.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
container.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
container.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
contentView.addSubview(text)
text.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 25).isActive = true
text.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 25).isActive = true
text.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -25).isActive = true
text.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -25).isActive = true
override init(frame: CGRect) {
super.init(frame: frame)
setUpViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}