Change UICollectionViewCell size on different device orientations

后端 未结 6 601
刺人心
刺人心 2020-12-12 09:36

I am using an UICollectionView with UICollectionViewFlowLayout.

I set the size of each cell through the

collectionView:lay         


        
6条回答
  •  伪装坚强ぢ
    2020-12-12 10:42

    Swift : Collection View Cell that is n% of screen height

    I needed was a cell that was a certain percentage of views height, and would resize with device rotation.

    With help from above, here is the solution

    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false
        // if inside nav controller set to true
    }
    
    override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
        collectionViewLayout.invalidateLayout()
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 100, height: collectionView.frame.height * 0.9)
    }
    

提交回复
热议问题