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

ios – 停止单个UICollectionView单元格流向屏幕中心,第1张

概述我试图理解为什么Collection View保持中心对齐集合中的最后一个单元格.  我创建了一个简单的基于Flow布局的集合视图.我正在使用Autolayout标志 – 我不确定是否会导致此问题. 每当我从Collection视图中删除一个单元格时 – 前几个似乎工作正常并向左滚动.然而,当我移除倒数第二个时,突然最后一个单元从左对齐变为中心对齐. 有人有解释吗?这看起来很奇怪. 如何制作它以使 我试图理解为什么Collection VIEw保持中心对齐集合中的最后一个单元格.
 我创建了一个简单的基于Flow布局的集合视图.我正在使用autolayout标志 – 我不确定是否会导致此问题.

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

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

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

这是视图行为

我做了一个简单的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.记录了一个关于使用UICollectionVIEwFlowLayoutautomaticSize时我注意到有关细胞行为变化的奇怪行为的Apple的错误
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    }

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

let layout = MyleftCustomFlowLayout() layout.estimatedItemSize = UICollectionVIEwFlowLayoutautomaticSizemyCollectionVIEwReference.collectionVIEwLayout = layout

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

总结

以上是内存溢出为你收集整理的ios – 停止单个UICollectionView单元格流向屏幕中心全部内容,希望文章能够帮你解决ios – 停止单个UICollectionView单元格流向屏幕中心所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1073294.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存