gpt4 book ai didi

ios - UICollectionView 单元格部分可见

转载 作者:行者123 更新时间:2023-11-29 05:56:36 25 4
gpt4 key购买 nike

我想制作一个可以水平滚动的collectionView,并且只有一个单元格项目可见,一个单元格项目部分可见。单元格应占据 collectionView 的整个高度。

我玩过collectionView的sectionInset。但没能达到我的目标。

我的代码:

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

目标(类似于此处的collectionView)

Something similar to this

我的输出

This is my output

最佳答案

使用这段代码

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.view.frame.width - 100
let height = self.view.frame.height
return CGSize(width: width, height: height)
}

不要忘记添加此委托(delegate)

UICollectionViewDelegateFlowLayout

关于ios - UICollectionView 单元格部分可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55088388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com