Ios UICollectionView居中分页,用于小于CollectionView';s宽度
我试图让我的单元格在屏幕宽度的0.85%左右,这样下一个和上一个单元格就会部分显示出来,告诉用户还有更多的单元格 我尝试使用这里的许多解决方案以及Ios UICollectionView居中分页,用于小于CollectionView';s宽度,ios,swift,uicollectionview,uiscrollview,uikit,Ios,Swift,Uicollectionview,Uiscrollview,Uikit,我试图让我的单元格在屏幕宽度的0.85%左右,这样下一个和上一个单元格就会部分显示出来,告诉用户还有更多的单元格 我尝试使用这里的许多解决方案以及collectionView.isPaginEnabled=true,但可见单元格从不居中 我希望显示的主单元格居中,无论其左/右是否有更多单元格 这是错误的行为和我的代码。谢谢你的帮助 视频: 这个问题本身并不是一个完整的解决方案,但我看到了一些可能会有所帮助的方法 实现您自己的自定义集合视图布局,这样您就可以完全控制单元格布局和属性,并且可以创建
collectionView.isPaginEnabled=true
,但可见单元格从不居中
我希望显示的主单元格居中,无论其左/右是否有更多单元格
这是错误的行为和我的代码。谢谢你的帮助
视频:
这个问题本身并不是一个完整的解决方案,但我看到了一些可能会有所帮助的方法
override func viewDidLoad() {
super.viewDidLoad()
// View preparation
view.addSubview(mainTabBarView)
mainTabBarView.configure()
// Collection view
mainTabBarView.collectionView.dataSource = self
mainTabBarView.collectionView.delegate = self
}
extension MainTabBarController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cells.previewCell, for: indexPath) as! PreviewCell
cell.configure()
return cell
}
}
extension MainTabBarController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width*0.85, height: collectionView.frame.size.height)
}
}
extension MainTabBarController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 15
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 15
}
}
class MainTabBarView {
// MARK:- Main configuration
func configure() {
...
setupCollectionView()
...
}
private func setupCollectionView() {
let flowLayout = UICollectionViewFlowLayout()
let screenWidth = UIScreen.main.bounds.width
flowLayout.itemSize = CGSize(width: Int(screenWidth * 0.85), height: 68)
flowLayout.minimumInteritemSpacing = 15
flowLayout.minimumLineSpacing = 15
flowLayout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: CGRect(), collectionViewLayout: flowLayout)
collectionView.backgroundColor = Colors.Primary.clear
collectionView.showsHorizontalScrollIndicator = false
collectionView.register(PreviewCell.self, forCellWithReuseIdentifier: Cells.previewCell)
collectionView.allowsSelection = false
collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
collectionView.isPagingEnabled = true
}
}