【发布时间】:2015-10-24 06:49:21
【问题描述】:
我正在为 iOS 8+ 编写代码。
我有一个UICollectionReusableView,它被用作UICollectionView 的标头
class UserHeader: UICollectionReusableView {
...
}
My View Collection 做了几件事情:
在viewDidLoad中加载NIB
override func viewDidLoad() {
super.viewDidLoad()
resultsCollectionView.registerNib(UINib(nibName: "UserHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader")
}
在referenceSizeForHeaderInSection 中设置标题高度。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(0, 500)
}
但是,我的UserHeader 视图由许多 UILabel、UIViews 组成,它们的高度在运行时会发生变化,我如何为 referenceSizeForHeaderInSection 指定一个动态的高度?或者,如果我不应该在 iOS 8+ 中使用 referenceSizeForHeaderInSection 进行自动调整大小,请告诉我应该使用什么。提前致谢。
为了完整起见,这是我加载视图的方式,但我不确定这是否与本次讨论相关:
func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {
var reusableview:UICollectionReusableView = UICollectionReusableView()
if (kind == UICollectionElementKindSectionHeader) {
let userHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader", forIndexPath: indexPath) as! UserHeader
... extra code to modify UserHeader
reusableview = userHeaderView
}
return reusableview
}
【问题讨论】:
标签: ios swift uicollectionview