UICollectionView contentOffset after device rotation

前端 未结 4 503
遇见更好的自我
遇见更好的自我 2020-12-13 15:36

What I am trying to do:

On Apple\'s Photo\'s App, if you rotate device while scroll to an arbitrary offset, same cell that was in the center beforehand would end u

4条回答
  •  孤街浪徒
    2020-12-13 16:33

    Accepted answer did not work for me. In my case neither method of UICollectionViewDelegate

    collectionView(_:targetContentOffsetForProposedContentOffset:)

    nor the method of UICollectionViewLayout was called

    targetContentOffset(forProposedContentOffset:)

    Instead, I inherited from UICollectionView and overrode layoutSubviews method:

    class CollectionView: UICollectionView {
        override func layoutSubviews() {
            let old = contentOffset
            super.layoutSubviews()
            contentOffset = old
        }
    }
    

    This may cause some side effects, so check it carefully before use in production

提交回复
热议问题