В моем приложении у меня есть UICollectionViewController
, для которого я меняю UICollectionViewLayout
. Пользовательский UICollectionViewFlowLayout
изменяет только высоту для ячеек. Проблема в том, что при установке нового UICollectionViewFlowLayout
положение в моих UICollectionView
полностью меняется, особенно дальше. Моя цель - добиться того, что UICollectionViewCell
, находящийся наверху (но не скрытый навигационной панелью или даже выше), остается в верхней части окна после обновления UICollectionViewLayout
.Как увеличить высоту UICollectionViewCell и оставаться в том же положении в ScrollView?
Вот код, о том, как я пытался ее решить:
class PacksCollectionViewLayout: UICollectionViewFlowLayout {
convenience init(height: CGFloat) {
self.init()
itemSize = CGSize(width: 80, height: height)
minimumInteritemSpacing = 10
minimumLineSpacing = 10
sectionInset.left = 10
sectionInset.right = 10
scrollDirection = .vertical
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
return collectionView!.contentOffset
}
}
class PacksCollectionViewController: UICollectionViewController {
var largeCells = false {
let height:CGFloat = largeCells ? 110 : 80
var lowestIndexPath:IndexPath? {
return self.collectionView?.indexPathsForVisibleItems.min()
}
UIView.animate(withDuration: 0.3){
self.collectionView?.collectionViewLayout.invalidateLayout()
self.collectionView?.setCollectionViewLayout(PacksCollectionViewLayout(height: height), animated: true)
if let index = lowestIndexPath{
self.collectionView?.scrollToItem(at: index, at: .top, animated: false)
}
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCell(withReuseIdentifier: PacksCollectionViewCell.identifier, for: indexPath)
}
}
При попытке перехода к UICollectionViewCell
, который указан как самый низкий IndexPath
в collectionView?.indexPathsForVisibleItems
движется способ далеко вверх, потому что эти клетки, по-видимому все еще видимый тоже. Какие-либо решения в этом отношении?
'yourCollectionView' имеет параметр' indexPath', но он никогда не используется в функции. Может быть, функция неполна? – Christoph