Abhishek Mitra
Abhishek Mitra

Reputation: 3395

Paging UICollectionView Cell

I'm building an app, where UICollectionView Lies on UITableViewCell, Here I want CollectionView should scroll horizontally, which is happening but i need here it to paging like one cell at a time in a fix position, but what I got here is by scrolling horizontally it scrolls but previous cell displaying little and the display portion become more on passing every cell.

Here is the Demo I have made, please go through the link and let me know where I'm doing wrong, and what to do to get the result as I wanted.

Link: https://drive.google.com/file/d/13k3JyaWiq1FuzGK4_8WNguXTUYwb-Xu6/view?usp=sharing

Thanks, Any help will be appreciable.

Upvotes: 0

Views: 613

Answers (2)

Abhishek Mitra
Abhishek Mitra

Reputation: 3395

Well I found the Answer, By putting this delegates Method of UICollectionViewDelegateFlowLayout

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.size.width - 10, height: collectionView.frame.size.height)
    }

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

Upvotes: 0

Ashwin Shrestha
Ashwin Shrestha

Reputation: 528

For this implement, UICollectionViewDelegateFlowLayout method

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

    let width  = (collectionView.frame.size.width)
    return CGSize(width: width, height: your tableview height here.. or collectionview cell height)

}

also in interfacebuilder of that tableview cell which contains your collectionview, select collectionview and set minimum spacing 0 for both lines and cells and also section insets to 0 as well

interface builder image

and also make sure that in interface builder you have set scrolldirection horizontal and pagining enabled true for the collectionview

Upvotes: 1

Related Questions