Sajib Ghosh
Sajib Ghosh

Reputation: 418

How to remove the gap between header and cell of UICollectionView?

I have a UICollectionView and it has a header and a cell. I want to remove the gap between the header and the cell.. How to do that in swift?

Here is my view...

enter image description here

I added background colour to the collectionView and header and cell also..

Please see the screenshot.

enter image description here

Upvotes: 4

Views: 3830

Answers (7)

Ofir Malachi
Ofir Malachi

Reputation: 1286

Custom your UIEdgeInsetsMake:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,0,0,0);  //{top, left, bottom, right};
}

Upvotes: 0

Imran Rasheed
Imran Rasheed

Reputation: 956

Here is my sample code you have to set UIEdgeInsets for spacing.

private let collectionViewLayout: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
    layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.showsVerticalScrollIndicator = false
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.register(TopExpertCustomCell.self, forCellWithReuseIdentifier: "TopExpertCollectionViewCell")
    collectionView.backgroundColor = UIColor(hex: "#F1F1F1")
    return collectionView
}()

Upvotes: 0

UdayM
UdayM

Reputation: 1783

Use the property section Inset of UICollectionViewFlowLayout

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
// Here you can set according your requirement

enter image description here

For more reference: http://www.brianjcoleman.com/tutorial-collection-view-using-swift/

Upvotes: 6

Alvin George
Alvin George

Reputation: 14296

try this:

   override func viewWillAppear(animated: Bool) {
self.automaticallyAdjustsScrollViewInsets = false;
}

On UICollectionViewDelegate:

 let k_cViewPadding = 8.0

func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            let cellDiffference:CGFloat = CGFloat(k_cViewPadding*4)
            return(CGSizeMake((collectionView.frame.size.width-cellDiffference)/3, (collectionView.frame.size.width-cellDiffference)/3))
    } 

Upvotes: 0

Sajib Ghosh
Sajib Ghosh

Reputation: 418

I Forgot to set section Inset to zero. It solved my problem. Thanks everyone.

Upvotes: 0

user5938635
user5938635

Reputation:

use the property sectionInset of UICollectionViewFlowLayout:

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 1;

in Swift:

var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1

Upvotes: 1

Payal Maniyar
Payal Maniyar

Reputation: 4402

Attaching screenshot of storyboard screen. Set as per that your storyboard and check. Hope your problem will be solved.

enter image description here

Upvotes: 0

Related Questions