根据内容居中水平 UICollectionView [英] Center horizontal UICollectionView based on content
问题描述
我有一个工作正常的水平 UICollectionView.但是当不需要滚动时,我想将单元格居中.即
I have a horizontal UICollectionView which works fine. But I want to center cell when there is no need for scrolling. i.e.
- 如果所有单元格都能够适应视图宽度 &用户不需要滚动然后将单元格居中
- 如果所有单元格都无法适应视图宽度 &用户需要滚动然后不要将单元格居中(默认行为)
- if all cell are able to fit within the view width & user don't need to scroll then Do center the cell
- if all cell are not able to fit within the view width & user need to scroll then Don't center the cell (default behaviour)
图形示例:
- 需要滚动然后保持默认行为的 UICollectionView
- 不需要滚动的 UICollectionView
到目前为止我所做的尝试:
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var dataList:[String] = ["icon_rotate", "icon_rotate2", "icon_rotate3", "icon_rotate4", "icon_rotate5", "icon_rotate6"]
override func viewDidLoad() {
super.viewDidLoad()
//other stuff
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return (UIDevice.current.userInterfaceIdiom == .pad) ? 20.0 : 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return (UIDevice.current.userInterfaceIdiom == .pad) ? 20.0 : 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.height * 0.75, height: collectionView.frame.size.height * 0.75)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "editorCell", for: indexPath) as! EditorCell
cell.iconView.image = UIImage(named: dataList[indexPath.row])
return cell
}
}
这导致 UICollectionView 的默认行为,所以我在 SO 中搜索并找到了这篇文章:如何水平居中 UICollectionView 单元格?我添加了这段代码:
This results in default behavior of UICollectionView so I searched in SO and found this post: How to center horizontally UICollectionView Cells? and I added this code:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
let totalCellWidth = CellWidth * CellCount
let totalSpacingWidth = CellSpacing * (CellCount - 1)
let leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
let rightInset = leftInset
return UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: rightInset)
}
当要加载的单元格很少时,此代码可以正常工作,但我希望它能够动态检测是否需要居中或让默认行为保持有效.
This code does work fine when there's few cells to be loaded but I want this to dynamically detect if centering is needed or let the default behavior stay in action.
谢谢
推荐答案
假设您当前的代码在您的单元格适合时工作 - 也就是说,它将单元格行"居中需要时 - 更改此行:
Assuming your current code is working when you the cells will fit - that is, it centers the "row of cells" when desired - change this line:
let leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
到:
let leftInset = max(0, (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2.0)
这样你的插图永远不会小于零
That way your insets will never be less than Zero
这篇关于根据内容居中水平 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!