Keeping the contentOffset in a UICollectionView while rotating Interface Orientation

后端 未结 24 1361
野性不改
野性不改 2020-12-04 07:22

I\'m trying to handle interface orientation changes in a UICollectionViewController. What I\'m trying to achieve is, that I want to have the same contentOffset afte

24条回答
  •  温柔的废话
    2020-12-04 08:17

    in Swift 3.

    you should track which cell item(Page) is being presented before rotate by indexPath.item, the x coordinate or something else. Then, in your UICollectionView:

    override func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
    
        let page:CGFloat = pageNumber // your tracked page number eg. 1.0
        return CGPoint(x: page * collectionView.frame.size.width, y: -(topInset))
        //the 'y' value would be '0' if you don't have any top EdgeInset
    }
    

    In my case I invalidate the layout in viewDidLayoutSubviews() so the collectionView.frame.size.width is the width of the collectionVC's view that has been rotated.

提交回复
热议问题