12

My goal is to create a UICollectionView with a layout as depicted below.

Is this possible without creating a custom UICollectionViewLayout?

Ujesh
  • 1,698
  • 2
  • 23
  • 35

4 Answers4

10

Use UICollectionViewDelegateFlowLayout method.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        let width : CGFloat
        let height : CGFloat

        if indexPath.item == 0 {
            width = 100
            height = 100
        } else {
            width = 50
            height = 50
        }
        return CGSizeMake(width, height)

}
Dejan Atanasov
  • 1,202
  • 10
  • 13
5

Without creating a custom layout of collection view it is not possible to achieve what you want.

You can create custom cell size using its delegate method sizeforitematindexpath.

Also, check this link which helps to create a custom layout.

Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
  • Okey, Still i am looking for simple way like scale cell size. custom layout is last option for me. Thank you – Ujesh May 26 '17 at 05:41
  • @Ujesh CollectionView is meant to be used with the layout. There is no other way you would achieve it. Else another way would be ScrollView + UIView which would make it difficult to manage things. – Parth Adroja May 26 '17 at 06:31
0

many ways to do this below links.

  • 1
    i know about this library, need simple Solution. -> Is this possible without creating a custom UICollectionViewLayout? – Ujesh May 25 '17 at 17:15
0

The answer from @Dejan Atanasov but for Swift 5

extension MainLobbyVC: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width : CGFloat
        let height : CGFloat
        if indexPath.item == 0 {
            width = 100
            height = 100
        } else {
            width = 50
            height = 50
        }
        return CGSizeMake(width, height)
    }
}
Tiago Mendes
  • 4,572
  • 1
  • 29
  • 35