0

I've got this extension for my collection view that makes horizontal scroll but I want to change it on vertical scroll??

extension viewRe : UIScrollViewDelegate
{
    func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
    {
        let layout = self.recipesCollView?.collectionViewLayout as! UICollectionViewFlowLayout
        let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing

        var offset = targetContentOffset.memory
        let index = (offset.x + scrollView.contentInset.left) / cellWidthIncludingSpacing
        let roundedIndex = round(index)

        offset = CGPoint(x: roundedIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
        targetContentOffset.memory = offset
    }
}

With this extension I'm trying to make the cells to stick to the TOP of the view even when he scrolls because the paging is enabled. So whenever the user scrolls i would like the cell to stick to the top and so on when the user scrolls.

Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
  • Have you tried setting the collection view's scroll direction? Not sure why you're implementing an extension actually. – donnywals Aug 11 '16 at 09:14
  • @donnywals yes the scrolling is enabled and also the paging – Konstantinos Natsios Aug 11 '16 at 09:16
  • But have you set the scroll direction? Since you're trying to convert from horizontal scrolling to vertical you probably want to set the `scrollDirection` to `.Vertical` – donnywals Aug 11 '16 at 09:17
  • You updated the question but I'm not really sure what you're asking right now.. You want to convert the scroll direction of your collection view from horizontal to vertical and your extension should make cells stick to the top? What have you tried so far? If you made it work horizontally wouldn't vertically mean you should change the x and left parts to y and top? – donnywals Aug 11 '16 at 09:36
  • @donnywals yes i've tried it but still it doesnt work this way – Konstantinos Natsios Aug 11 '16 at 09:39

1 Answers1

2

@donnyWals is right. If you are using a UICollectionView just change its UICollectionViewFlowLayout

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = . Horizontal
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)

or if you have an existent UICollectionView

if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.scrollDirection = .Horizontal
}

Follow the official API:

UICollectionView

UICollectionViewFlowLayout

gabriel
  • 2,351
  • 4
  • 20
  • 23