專案是一個即時聊天的社交軟體,聊天流采用的是UICollectionView,隨著進度的完善,發現一個特別的bug,UICollectionviewCell的復用,并沒有直接insert進去,而是出現了莫名奇妙的插入方式,
這不是我的圖,這是我在網上找到的,跟我的效果一樣一樣的,link the image
起初我以為是影片出了問題,導致keyboard跟UICollectionView起了沖突,可后來發現,關閉了所有的影片也不起作用,折騰了一天,終于在stackOverflow上找到了答案,
https://stackoverflow.com/questions/56584816/swift-uicollectionview-cell-displayed-scrolled-in-to-the-view-incorrectly-when-k
這位大佬的情況跟我的一樣一樣的,可他沒有給出具體的解決辦法,只是提出了一些寶貴建議,
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.
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!
抽重點:設定collectionView限制超出顯示邊緣,這迫使呼叫顯示單元格更早,并給一些時間,以奠定適當的單元格,甚至隱藏鍵盤影片使用,頂部和底部的contentInset必須在viewDidLoad中設定,并在運行時相應地處理任何contentInset的變化
(我做的是,將底部約束加+1,超出父類的bottom,這樣解決了collectionview莫名其妙的插入,但是這樣卻寫死了collectionview的約束,無法使串列跟隨鍵盤改變contentInset,如果約束<1,又會出現奇怪問題,我也不知這是為何,)
經過一番折騰之后,我發現通過主執行緒延時更改collectionview的contentInset也能有效解決串列的重繪問題,
補充:1、如何解決UICollectionView進行reload時,閃爍問題?
傻逼方法:reload之前先設定alpha = 0,結束后alpha = 1
// self.messagesCollectionView.reloadData()// self.messagesCollectionView.performBatchUpdates({// }) { (finish) in// self.messagesCollectionView.alpha = 1// }
補充 :2、解決UITableView或UICollectionView中cell影響UIButton的高亮效果
解決辦法:
tableView.delaysContentTouches = false
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/7512.html
標籤:iOS
上一篇:【iOS翻譯】對UIGestureRecognizer多種手勢傻傻分不清
下一篇:iOS核心影片高級技巧 - 6
