[英]How to remove the gap between header and cell of UICollectionView?
使用UICollectionViewFlowLayout
的屬性部分 Inset
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
// Here you can set according your requirement
更多參考: http : //www.brianjcoleman.com/tutorial-collection-view-using-swift/
使用UICollectionViewFlowLayout
的屬性sectionInset
:
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 1;
在斯威夫特:
var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1
這是我的示例代碼,您必須為間距設置UIEdgeInsets
。
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
}()
我忘記將部分插入設置為零。 它解決了我的問題。 謝謝大家。
試試這個:
override func viewWillAppear(animated: Bool) {
self.automaticallyAdjustsScrollViewInsets = false;
}
在 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))
}
自定義您的 UIEdgeInsetsMake:
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,0,0,0); //{top, left, bottom, right};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.