根据内容将水平UICollectionView居中 [英] Center horizontal UICollectionView based on content

查看:85
本文介绍了根据内容将水平UICollectionView居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平的UICollectionView,它工作正常.但是我想在不需要滚动时将单元居中.即

I have a horizontal UICollectionView which works fine. But I want to center cell when there is no need for scrolling. i.e.

  1. 如果,所有单元格均适合视图宽度&用户不需要滚动然后将单元格居中
  2. 如果所有单元格都无法容纳在视图宽度&用户需要滚动然后不要将单元格居中(默认行为)
  1. if all cell are able to fit within the view width & user don't need to scroll then Do center the cell
  2. if all cell are not able to fit within the view width & user need to scroll then Don't center the cell (default behaviour)

图形示例:

  1. 需要滚动然后保持默认行为的UICollectionView

  1. 不需要滚动的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆