如何在Swift3.0中居中对齐UICollectionView的单元格? [英] How to center align the cells of a UICollectionView in Swift3.0?

查看:661
本文介绍了如何在Swift3.0中居中对齐UICollectionView的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答 Objective-C Swift2.0 如何居中对齐细胞UICollectionView?

我通常会尝试将 Swift2.0 的答案转换为 Swift3.0 解决方案,但是方法:

I usually would try to convert the Swift2.0 answer to the Swift3.0 solution, however the method:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        let edgeInsets = (screenWight - (CGFloat(elements.count) * 50) - (CGFloat(elements.count) * 10)) / 2
        return UIEdgeInsetsMake(0, edgeInsets, 0, 0);
    }

似乎不存在于 Swift3.0中,我发现的唯一有用的其他方法是:

doesn't seem to exist in Swift3.0,and the only other method I found that seems useful is:

func collectionView(_ collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
        <#code#>
}

但我不确定如何正确实施它。

but I am unsure how to implement it correctly.

如何将 UICollectionView 的单元格居中对齐 Swift3.0

How to center align the cells of a UICollectionView in Swift3.0?

(iOS和tvOS的简单通用解决方案将是完美的)

(A simple and general solution for iOS and tvOS would be perfect)

推荐答案

这最终成为我使用的解决方案。阅读代码注释以便更好地理解。 Swift 3.0

This ended up being the solution I used. Read the code comments for a better understanding. Swift 3.0

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        //Where elements_count is the count of all your items in that
        //Collection view...
        let cellCount = CGFloat(elements_count)

        //If the cell count is zero, there is no point in calculating anything.
        if cellCount > 0 {
            let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
            let cellWidth = flowLayout.itemSize.width + flowLayout.minimumInteritemSpacing

            //20.00 was just extra spacing I wanted to add to my cell.
            let totalCellWidth = cellWidth*cellCount + 20.00 * (cellCount-1)
            let contentWidth = collectionView.frame.size.width - collectionView.contentInset.left - collectionView.contentInset.right

            if (totalCellWidth < contentWidth) {
                //If the number of cells that exists take up less room than the 
                //collection view width... then there is an actual point to centering them.

                //Calculate the right amount of padding to center the cells.
                let padding = (contentWidth - totalCellWidth) / 2.0
                return UIEdgeInsetsMake(0, padding, 0, padding)
            } else {
                //Pretty much if the number of cells that exist take up
                //more room than the actual collectionView width, there is no
                // point in trying to center them. So we leave the default behavior.
                return UIEdgeInsetsMake(0, 40, 0, 40)
            }
        }

        return UIEdgeInsets.zero
    }

这篇关于如何在Swift3.0中居中对齐UICollectionView的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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