【发布时间】:2020-05-01 22:46:24
【问题描述】:
我正在努力调整集合视图的大小。
目前我有一个计算大小的流布局方法:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let cell = collectionView.cellForItem(at: indexPath) {
let pokemonCell = cell as! PokemonCell
pokemonCell.setNeedsDisplay()
}
return CGSize(width: 150, height: collectionView.bounds.size.height)
}
还有两种调整旋转大小的方法:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}
flowLayout.invalidateLayout()
self.collectionView.reloadData()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.frame = self.view.bounds
self.collectionView.reloadData()
}
单元格内容在容器中居中。当我更改布局时,单元格内容开始跳跃。其中一些重新定位自己,一些保留在先前布局的 y 位置。有时内容会得到正确的位置,然后会在滚动时返回。
【问题讨论】:
标签: swift uicollectionview uikit uicollectionviewflowlayout