In my collectionview I have two layouts, one list and one grid (two colum).
When I have list activated there is a 2px space between the cell and the screen on each side.
But when I change to grid layout I only get a small space between my cells and not the cells and the screen. I would like to add a 2px space between the cell and the screen.
Code for two colum grid:
class GridLayout: UICollectionViewFlowLayout {
var numberOfColumns: Int = 3
init(numberOfColumns: Int) {
super.init()
minimumLineSpacing = 1
minimumInteritemSpacing = 1
self.numberOfColumns = numberOfColumns
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var itemSize: CGSize {
get {
if let collectionView = collectionView {
let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - self.minimumInteritemSpacing
let itemHeight: CGFloat = 260.0
return CGSize(width: itemWidth, height: itemHeight)
}
// Default fallback
return CGSize(width: 100, height: 100)
}
set {
super.itemSize = newValue
}
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
return proposedContentOffset
}
}
And code for list which works:
class ListLayout: UICollectionViewFlowLayout {
var itemHeight: CGFloat = 180
init(itemHeight: CGFloat) {
super.init()
minimumLineSpacing = 1
minimumInteritemSpacing = 1
self.itemHeight = itemHeight
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var itemSize: CGSize {
get {
if let collectionView = collectionView {
//Adding -4 to set a 2px space between cell and screen
let itemWidth: CGFloat = collectionView.frame.width - 4
return CGSize(width: itemWidth, height: self.itemHeight)
}
// Default fallback
return CGSize(width: 100, height: 100)
}
set {
super.itemSize = newValue
}
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
return proposedContentOffset
}
}
Change your init of UICollectionViewFlowLayout to this:-
init(numberOfColumns: Int) {
super.init()
minimumLineSpacing = 1
minimumInteritemSpacing = 1
self.numberOfColumns = numberOfColumns
let leftPadding: CGFloat = 2.0
let rightPadding: CGFloat = 2.0
sectionInset = UIEdgeInsetsMake(0, leftPadding, 0, rightPadding)
}
And change your itemSize getter method to this:-
get {
if let collectionView = collectionView {
let padding: CGFloat = 2.0
let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(self.numberOfColumns)) - (self.minimumInteritemSpacing+ 2*padding)
let itemHeight: CGFloat = 260.0
return CGSize(width: itemWidth, height: itemHeight)
}
// Default fallback
return CGSize(width: 100, height: 100)
}
Below is how you set custom cell spacing in a collectionview. You have to create an extension.
extension ViewController : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return //whatever you want in CGSize(width:, height:)//
//so if you want 2px before screen, between cells, and after cell, you would put (self.view.bounds.width - 6) / 2 for width
}
Make sure the "ViewController" that follows extension is the name of your viewcontroller.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(2,2,2,2) }
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(2,2,2,2);
} this code may help u.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.