0

I want to set custom horizontal scroll for my CollectionView, that it will be scrolling by 1 cell (not by the whole width of my screen).

I could set HORIZONTAL scroll, but not custom. (See screens). 1 screen: my extension of my collectionView for UIScrollViewDelegate. *I saw, that in console (see too) "x" - my offset = 290 - it's true! But on fact it is not 290. Paging was marked in "true" 2 screen: delegate and dataSource.

enter image description here

Help, please!

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Possible duplicate of [How can I change the scroll direction in UICollectionView?](https://stackoverflow.com/questions/18464153/how-can-i-change-the-scroll-direction-in-uicollectionview) – Kiran Sarvaiya Nov 10 '17 at 12:21

3 Answers3

0

First you need to set your collectionView scroll direction horizontal (UICollectionViewScrollDirectionHorizontal) and set pagingEnabled to YES

It means your collection view will be scroll horizontally with one by one cell.

Set horizontal direction (Select collection view from XIB or Storyboard)

enter image description here

And for set pagination enable

myCollectionView.pagingEnabled = true

Updated :

You should use of UICollectionViewDelegateFlowLayout delegate

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let width = UIScreen.main.bounds.size.width
        var height = 101.7 // set height as you want
        return CGSize(width: width, height: height)
    }
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

If using storyBoard the go to collection view and select the right handed tab "show the size inspector" and change

minimum spacing for cells = 0
minimum spacing for lines = 0

if you want to change directly from code then implement these UICollectionViewDelegateFlowLayout methods

 func collectionView(_ collectionView: UICollectionView, layout 
       collectionViewLayout: UICollectionViewLayout, 
   minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

must be remember scrolling direction set horizontal and pagination enabled

and implement this delegate methods into code

  extension ViewController: UICollectionViewDelegateFlowLayout {
 func collectionView(_ collectionView: UICollectionView, layout 
 collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: 
 IndexPath) -> CGSize {
      return CGSize(width: collectionView.frame.size.width, height: 
collectionView.frame.size.height)
     }
 }
Aman Gupta
  • 159
  • 1
  • 8
-2

You could create a custom UICollectionViewFlowLayout that will center the cells in the screen and then page one at a time. Karmadust have written a good blog post that I have successfully used in the past to do the same thing

http://blog.karmadust.com/centered-paging-with-preview-cells-on-uicollectionview/

Edward Ford
  • 1,631
  • 3
  • 13
  • 25