By user11044406


2019-02-11 09:44:27 8 Comments

I need to create a collectionView to display a series of images, individually for each photo. I'm trying to reproduce what you see in the picture, view a photo and then scroll left or right for the next. I can not create the layout of the collectionView

example image

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.datasource.count
}

//UICollectionViewDelegateFlowLayout - constraint della collecion view da innestare
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = (UIScreen.main.bounds.size.width - 3) / 3
    let height = width
    return CGSize(width: width, height: height)
}

1 comments

@Hiền Đỗ 2019-02-11 10:04:15

swift 4:

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

     //calculation of cell size
    return CGSize(width: self.collectionview.frame.size.width,height: 180)
      }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    // indentation 
    return UIEdgeInsetsMake(0, 23, 20, 20)
    }

If you want to drag to horizontal or vertical, in collectionview there is a "scroll Direction" attribute. You can change it to horizontal or vertical.

@Galo Torres Sevilla 2019-02-11 10:36:59

why would you divide by 2? Full width is needed here

@Hiền Đỗ 2019-02-11 11:03:54

i have edited it. thank you so much

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] Swift Error - Use of undeclared type 'cell' - Collection View

19 Answered Questions

[SOLVED] #pragma mark in Swift?

  • 2014-06-03 14:05:56
  • Arbitur
  • 209049 View
  • 898 Score
  • 19 Answer
  • Tags:   swift

1 Answered Questions

[SOLVED] Swift vertical UICollectionView inside UITableView

2 Answered Questions

How to show images from API in CollectionView

2 Answered Questions

1 Answered Questions

0 Answered Questions

Resize View height on didSelectItemAt Swift

2 Answered Questions

[SOLVED] Issues with casting celltypes to dequeue

  • 2017-01-02 13:45:12
  • Valerio Zhang
  • 324 View
  • 1 Score
  • 2 Answer
  • Tags:   ios swift swift3

1 Answered Questions

1 Answered Questions

[SOLVED] My CollectionViewCell is getting into a mess

Sponsored Content