停止单个UICollectionView单元stream动到屏幕的中心

我想了解为什么集合视图保持中心只alignment集合中的最后一个单元格。
我创build了一个简单的基于stream布局的集合视图。 我使用的Autolayout标志 – 我不知道是造成这个问题。

每当我从收集视图中删除一个单元格 – 前几个似乎正常工作,滚到左侧。 但是,当我删除倒数第二个,然后突然最后一个单元格从左alignment更改为中心alignment。 有没有人有解释? 这似乎很奇怪。

我如何做到这一点,以便所有单元格将向左滚动并保持与左侧alignment。

编辑:这里是视图层次debugging: 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]) } 

我设法解决这个问题,使用以下两个步骤:1.logging了一个与苹果有关的错误行为我注意到使用UICollectionViewFlowLayoutAutomaticSize 2时改变了单元格行为的错误。我做了一个工作,通过创build一个修改CustomViewFlowLayout(找不到原来的SO / github问题,我看到这种方法使用 – 我会添加它,如果我find它)然后我添加了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 } 

在ViewController中 – 您必须将stream布局添加到集合视图才能使其工作:

 let layout = MyLeftCustomFlowLayout() layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize myCollectionViewReference.collectionViewLayout = layout 

我认为Collection View的实现绝对可以简化,但是我可以看到它的function有多强大。

当最后一个单元格(唯一)显示时,尝试查看debugging – > 捕获视图层次 (即假设你在模拟器中运行)。 我的预感:一旦你只剩下一个,你的牢房就会变得越来越大