0

В моем приложении у меня есть 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 движется способ далеко вверх, потому что эти клетки, по-видимому все еще видимый тоже. Какие-либо решения в этом отношении?

ответ

0

это, как я сделал это, я надеюсь, что это помогает, и жаль, если это не
так на макете потока добавить этот код

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    commonInit() 
} 

override init() { 
    super.init() 
    commonInit() 
} 

func commonInit() { 
    minimumLineSpacing = 10.0 
} 

fun yourCollectionView(collectionView: UICollectionView, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSize(width: collectionView.bounds.width, height: 230.0) 
} 

и на главном контроллере коллекции добавить

let layout = yourFlowLayout() 



    override func viewDidLoad() { 
    super.viewDidLoad() 

collectionView?.collectionViewLayout = layout 


func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return layout.yourCollectionView(collectionView, sizeForItemAtIndexPath: indexPath) 
} 

Я надеюсь, что это помогло :)

+0

'yourCollectionView' имеет параметр' indexPath', но он никогда не используется в функции. Может быть, функция неполна? – Christoph