在我的collectionview中,我有两个布局,一个列表和一个网格(两个列)。
当我激活列表时,单元格和屏幕两侧之间有2px的空间。
但是当我改为网格布局时,我的单元格之间只有一个小空间,而不是单元格和屏幕。我想在单元格和屏幕之间添加一个2px的空间。
两个colum网格的代码:
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
}
}
列表的代码有效:
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
}
}
答案 0 :(得分:1)
将 UICollectionViewFlowLayout 的 init 更改为: -
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)
}
将 itemSize getter方法更改为: -
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)
}
答案 1 :(得分:0)
以下是在集合视图中设置自定义单元格间距的方法。你必须创建一个扩展名。
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
}
确保" ViewController" 扩展名后面是viewcontroller的名称。
答案 2 :(得分:0)
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.