Enforce collectionView to have only 2 rows

后端 未结 3 1676
生来不讨喜
生来不讨喜 2020-12-06 22:14

I have to make one collection view so that irrespective of iphone size we have just 2 images in each row and also we need border between each row and column as shown in the

3条回答
  •  天涯浪人
    2020-12-06 22:44

    Implement the following functions from collectionView's protocol:

    // cell size
    
    func collectionView(collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: view.frame.size.width/2, height: view.frame.size.width/2)
    }
    

    ...where view is your controller's (super) view

    // inter-spacing
    
    func collectionView(collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 2.0
    }
    
    func collectionView(collectionView: UICollectionView, layout
            collectionViewLayout: UICollectionViewLayout,
            minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 2.0
    }
    

提交回复
热议问题