Liu*_*x31 20 ios uicollectionview swift
我想在标题和实际项目之间添加一些间距,目前看起来像这样:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
// Create header
switch kind{
case UICollectionElementKindSectionHeader:
let headerView = iconCollectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "customIconHeaderView", forIndexPath: indexPath) as! CustonIconHeaderView
headerView.setUp() //add whatever into the view
return headerView
default:
assert(false, "Unexpected element kind")
}
}
Run Code Online (Sandbox Code Playgroud)
ish*_*haq 28
您基本上是在为集合视图部分添加上边距,因为您将为该部分设置顶部插入.要在代码中执行此操作,请执行insetForSectionAtIndex:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 10.0, left: 1.0, bottom: 1.0, right: 1.0)
}
Run Code Online (Sandbox Code Playgroud)
如果你不想实现insetForSectionAtIndex,你也可以用适当的方法做这样的事情,例如viewDidLoad:
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 10.0, left: 1.0, bottom: 1.0, right: 1.0)
Run Code Online (Sandbox Code Playgroud)
在Interface Builder中,选择集合视图并更改Section Insets - > Top的值,如下图所示:
注意:这仅在您使用Flow Layout时有效.
您可以做的一种方法是使用以下方法增加标头容器的高度
collectionView(_:layout:referenceSizeForHeaderInSection:)
例子:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 0, height: yourHeaderContentHeight + yourHeaderMarginToCell)
}
Run Code Online (Sandbox Code Playgroud)
编辑:
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "YourID", forIndexPath: indexPath)
let yourCustomView = UIView(frame: CGRect(x: 0, y: 0, width: yourHeaderWidth, height: yourHeaderHeight))
headerView.addSubview(yourCustomView)
return headerView
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: yourHeaderWidth, height: yourHeaderHeight + yourHeaderMargin)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10762 次 |
| 最近记录: |