UICollectionView - resizing cells on device rotate - Swift

后端 未结 9 1573
醉梦人生
醉梦人生 2020-12-04 23:12

I\'ve created a UICollectionView, so that I can arrange views into neat columns. I\'d like there to be a single column on devices > 500 pixels wide.

In order to achi

9条回答
  •  粉色の甜心
    2020-12-05 00:04

    You might use viewWillLayoutSubviews. This question should be helpful but this is bassically called whenever the view controller views is about to layout its subviews.

    So your code will look like this:

    override func viewWillLayoutSubviews() {
      super.viewWillLayoutSubviews()
    
      guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
        return
      }
    
      if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
        //here you can do the logic for the cell size if phone is in landscape
      } else {
        //logic if not landscape 
      }
    
      flowLayout.invalidateLayout()
    }
    

提交回复
热议问题