[英]swift uicollectionview no space between cell and screen only between cells
在我的collectionview中,我有兩種布局,一種列表和一種網格(兩個列)。
當我激活列表時,單元格和屏幕兩側之間有2px的間隔。
但是,當我更改為網格布局時,單元格之間只有很小的空間,而單元格和屏幕之間沒有。 我想在單元格和屏幕之間添加2px的空間。
兩個哥倫格的代碼:
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
}
}
將您的UICollectionViewFlowLayout的初始化更改為:-
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)
}
以下是如何在collectionview中設置自定義單元格間距。 您必須創建一個擴展。
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的名稱。
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.