Reputation: 418
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...
I added background colour to the collectionView
and header and cell also..
Please see the screenshot.
Upvotes: 4
Views: 3830
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
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
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
For more reference: http://www.brianjcoleman.com/tutorial-collection-view-using-swift/
Upvotes: 6
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
Reputation: 418
I Forgot to set section Inset to zero. It solved my problem. Thanks everyone.
Upvotes: 0
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
Reputation: 4402
Attaching screenshot of storyboard screen. Set as per that your storyboard and check. Hope your problem will be solved.
Upvotes: 0