我想了解爲什麼集合視圖保持中心對齊集合中的最後一個單元格。
我已經創建了一個簡單的基於流佈局的集合視圖。我正在使用Autolayout標誌 - 我不確定是否導致此問題。停止單個UICollectionView單元流到屏幕中央
每當我從收集視圖中刪除一個單元格 - 前幾個似乎正常工作,滾到左側。但是,當我刪除倒數第二個時,突然最後一個單元從左對齊變爲中心對齊。 有沒有人有解釋?這似乎很奇怪。
我該如何製作,以便所有單元格都會向左滾動並保持與左側對齊。
編輯:這裏是視圖層次結構的調試: 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 UICollectionViewFlowLayoutAutomaticSize 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 UICollectionViewFlowLayoutAutomaticSize 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])
}
我以爲是這樣 - 但將單元格的背景顏色設爲紅色,並且您可以看到它保持相同的大小 –
不是爲了鞭策死馬,而是您嘗試捕獲視圖層次結構,然後單擊lil圖標以打開顯示主動約束?變成瘋狂的東西有時 – xaphod
謝謝@xaphod - 我不知道視圖調試 - 那很好。我已經完成了對2個單元格和1個單元格的視圖調試 - 看起來有一個新的約束被添加到中心 - ,但我不知道如何解決它。這是你認爲的錯誤嗎? http:// imgur。com/a/NxidO –