項目是一個即時聊天的社交軟件,聊天流採用的是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也能有效解決列表的刷新問題。