UICollectionView horizontal paging with space between pages

I am looking for a way to replace my own UIPageViewControllerhorizontal paging with UICollectionView.

So far I have done the following:

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = collectionView.frame.size
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 10

collectionView.setCollectionViewLayout(layout, animated: false)
collectionView.isPagingEnabled = true
collectionView.alwaysBounceVertical = false

this works fine and I get the effect of horizontal paging.

Now I want to add horizontal space between pages (for example, u will do with UIPageViewControllerOptionInterPageSpacingKeyon UIPageViewController)

so far I have not been able to distinguish between the method of adding spaces without prejudice to the paging effect. im looking for the same behavior as with UIPageViewController: the cell should fill the entire width of the screen, and the space between the cells should be visible only when switching the page.

+5
2

:

  • collectionView.isPagingEnabled = true false
  • minimumLineSpacing
  • targetContentOffsetForProposedContentOffset:withScrollingVelocity:, contentOffset . itemSize minimumLineSpacing, .

:

  • collectionView.isPagingEnabled = true
  • minimumLineSpacing
  • . collectionView , screenSize. , minimumLineSpacing 10, View {0, -5, width + 10, height}
  • contentInset , minimumLineSpacing, .
+6
  • UICollectionViewFlowLayout minimumLineSpacing - 10.0, View (itemSize.width = collectionView.bounds.width), collectionView - , "minimumLineSpacing" : , , .
  • , collectionView 'minimumLineSpacing', . pratice : , View "lineSpacing", "contentSize" , , , "minimumLineSpacing" 10.0, collectionView contentSize 10.0.
  • , (, UIScrollView): ViewItem "pageSpacing" (, 10.0) collectionView , . , , collevtionViewCell , .

, Swift 3.1, , :

let pageSpacing: CGFloat = 10

class ViewController: UITableViewController {
     override func viewDidLoad() {
        super.viewDidLoad()

        let layout = UICollectionViewFlowLayout()
        layout.itemSize                = CGSize(width: view.frame.width + pageSpacing, height: view.frame.height)
        layout.scrollDirection         = .horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing      = 0

        let frame = CGRect(x: 0, y: 0, width: view.frame.width + pageSpacing, height: view.frame.height)
        let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
        view.addSubview(collectionView)
    }
}

class MyCell: UICollectionViewCell {
    var fullScreenImageView = UIImageView()
    override func layoutSubviews() {
        super.layoutSubviews()
        fullScreenImageView.frame = CGRect(x: 0, y: 0, width: frame.width - pageSpacing, height: frame.height)
    }
}

, .

+1

Source: https://habr.com/ru/post/1680996/


All Articles