【iOS bug记录】UICollectionviewCell刷新变得这么莫名其妙?

 

  项目是一个即时聊天的社交软件,聊天流采用的是UICollectionView,随着进度的完善,发现一个特别的bug,UICollectionviewCell的复用,并无直接insert进去,而是出现了莫名奇妙的插入方式,swift

  这不是个人图,这是我在网上找到的,跟个人效果同样同样的。link the image app

  起初我觉得是动画出了问题,致使keyboard跟UICollectionView起了冲突,可后来发现,关闭了全部的动画也不起做用。折腾了一天,终于在stackOverflow上找到了答案。ide

  https://stackoverflow.com/questions/56584816/swift-uicollectionview-cell-displayed-scrolled-in-to-the-view-incorrectly-when-k动画

  这位大佬的状况跟个人同样同样的,可他没有给出具体的解决办法,只是提出了一些宝贵建议。ui

  After long investigation I noticed that the same phenomen happens also at the bottom in particular cases. I am not sure it it is bug or custom layout is necessary, but with the simple flow layout I solved the issue by setting collectionView contraints beyound the display edge.spa

  This force to call displaying cell earlier and gives some time to lay cell approprietely even hide keyboard animation is used. Of coarse collectionView top and bottom contentInset has to be set in viewDidLoad and handle any contentInset change during runtime accordingly线程

  Hope it help!get

  抽重点:设置collectionView限制超出显示边缘。这迫使调用显示单元格更早,并给一些时间,以奠基适当的单元格,甚至隐藏键盘动画使用。顶部和底部的contentInset必须在viewDidLoad中设置,并在运行时相应地处理任何contentInset的变化animation

  (我作的是,将底部约束加+1,超出父类的bottom,这样解决了collectionview莫名其妙的插入,可是这样却写死了collectionview的约束,没法使列表跟随键盘改变contentInset。若是约束<1,又会出现奇怪问题,我也不知这是为什么。)it

  通过一番折腾以后,我发现经过主线程延时更改collectionview的contentInset也能有效解决列表的刷新问题