停止单个UICollectionView单元格流向屏幕中心

UKD*_*eek 7 ios uicollectionview uicollectionviewlayout

我试图理解为什么Collection View保持中心对齐集合中的最后一个单元格.
我创建了一个简单的基于Flow布局的集合视图.我正在使用Autolayout标志 - 我不确定是否会导致此问题.

每当我从Collection视图中删除一个单元格时 - 前几个似乎工作正常并向左滚动.然而,当我移除倒数第二个时,突然最后一个单元从左对齐变为中心对齐.有人有解释吗?这看起来很奇怪.

如何制作它以使所有单元格向左滚动并保持与左对齐.

编辑:这是视图层次调试:http: //imgur.com/a/NxidO

这是视图行为

Collectionview居中

我已经制作了一个简单的github来演示它:https://github.com/grantkemp/CollectionViewIssue

这是代码:

        var dataforCV = ["short","longer phrase", "Super long Phrase"]

override func viewDidLoad() {
        super.viewDidLoad()
        demoCollectionView.reloadData()
    let layout = UICollectionViewFlowLayout()

    // Bug Description  - > UICollectionViewFlowLayoutAutomaticSize will center Align the layout if it has only a single cell - but it will left align the content if there is more than one cell.

    // I would expect this behaviour to be consistently left aligning the content.

    //How to repeat: If you comment out UICollection?View?Flow?Layout?Automatic?Size then the collection view will show 3 cells being left aligned and it will continue to left align all the content no matter how many cells you remove.

    // But: if you leave turn UICollection?View?Flow?Layout?Automatic?Size on - then it will intially show 3 cells being left aligned, and then as you click to remove each cell they will stay left aligned until the last single cell will suddenly center align in the collection view

    //  see here for the screen recording:https://i.stack.imgur.com/bledY.gif
    //  see here for the view hierachy debuggins screen: http://imgur.com/a/NxidO

    layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize


    // <-- End Bug Description
    demoCollectionView.collectionViewLayout = layout
}

    //MARk: CollectionView

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? collectionCell
        cell?.text.text  = dataforCV[indexPath.row]

        return cell!

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataforCV.count
    }
    //Remove the item from the array if selected
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        dataforCV.remove(at: indexPath.row)
        demoCollectionView.deleteItems(at: [indexPath])
    }
Run Code Online (Sandbox Code Playgroud)

UKD*_*eek 7

我设法使用以下两个步骤解决了这个问题:1.向Apple记录了一个关于我注意到在使用UICollectionViewFlowLayoutAutomaticSize 2时细胞行为发生变化的奇怪行为的错误.我通过创建修改后的CustomViewFlowLayout(无法找到)做了一些工作原始的SO/github问题,我看到这种方法使用 - 如果我找到它我会添加它)然后我添加了UICollectionViewFlowLayoutAutomaticSize设置 - 并且它工作得很漂亮.

示例代码:

class MyLeftCustomFlowLayout:UICollectionViewFlowLayout {
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

        let attributes = super.layoutAttributesForElements(in: rect)

        var leftMargin = sectionInset.left
        var maxY: CGFloat = 2.0

        let horizontalSpacing:CGFloat = 5

        attributes?.forEach { layoutAttribute in
            if layoutAttribute.frame.origin.y >= maxY
                || layoutAttribute.frame.origin.x == sectionInset.left {
                leftMargin = sectionInset.left
            }

            if layoutAttribute.frame.origin.x == sectionInset.left {
                leftMargin = sectionInset.left
            }
            else {
                layoutAttribute.frame.origin.x = leftMargin
            }

            leftMargin += layoutAttribute.frame.width + horizontalSpacing
            maxY = max(layoutAttribute.frame.maxY, maxY)
        }

        return attributes
    }
Run Code Online (Sandbox Code Playgroud)

在ViewController中 - 您必须将流布局添加到集合视图以使其工作:

let layout = MyLeftCustomFlowLayout() 
layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
myCollectionViewReference.collectionViewLayout = layout
Run Code Online (Sandbox Code Playgroud)

我认为Collection View的实现绝对可以简化,但我会更多地研究它,因为我可以看到它有多强大.


Abd*_*han 5

在斯威夫特 5

使用 UICollectionViewFlowLayout 将单元格向左对齐,如下所示

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
self.collectionView.collectionViewLayout = layout
Run Code Online (Sandbox Code Playgroud)


xap*_*hod 0

当最后一个单元格(仅)显示时,尝试查看调试 ->捕获视图层次结构(即假设您在模拟器中运行)。我的预感:一旦你只剩下一个细胞,你的细胞就会变大