How to center align the cells of a UICollectionView?

前端 未结 18 2471
梦如初夏
梦如初夏 2020-11-28 01:33

I am currently using UICollectionView for the user interface grid, and it works fine. However, I\'d like to be enable horizontal scrolling. The grid supports 8

18条回答
  •  执念已碎
    2020-11-28 01:54

    Another way of doing this is setting the collectionView.center.x, like so:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        if let
            collectionView = collectionView,
            layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
        {
            collectionView.center.x = view.center.x + layout.sectionInset.right / 2.0
        }
    }
    

    In this case, only respecting the right inset which works for me.

提交回复
热议问题