UICollectionView AutoSize标题高度

Dea*_*ean 7 ios uicollectionview swift

我正在为iOS 8+编写代码.

我有一个UICollectionReusableView被用作标题的UICollectionView

class UserHeader: UICollectionReusableView {
...
}
Run Code Online (Sandbox Code Playgroud)

我的视图集合做了一些事情:

加载NIB viewDidLoad

override func viewDidLoad() {
super.viewDidLoad()
resultsCollectionView.registerNib(UINib(nibName: "UserHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader")
}
Run Code Online (Sandbox Code Playgroud)

设置标题高度referenceSizeForHeaderInSection.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSizeMake(0, 500)
}
Run Code Online (Sandbox Code Playgroud)

但是,我的UserHeader观点包括许多UILabel,UIViews在运行时的高度变化,我如何指定referenceSizeForHeaderInSection动态的高度?或者,如果我不想referenceSizeForHeaderInSection在iOS 8+中使用自动调整大小,请告诉我应该使用的内容.提前致谢.

为了完整起见,这是我加载视图的方式,但我不确定这是否与此讨论相关:

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
}
Run Code Online (Sandbox Code Playgroud)

Inf*_*mes -1

我遇到了类似的问题,并通过NSLayoutConstraint在视图中使用 s 来指定标题视图的高度来修复它。然后,在视图控制器中,我这样配置集合视图的标头:

fileprivate var expectedSectionHeaderFrame = CGRect.zero
let headerIdentifier = "HeaderView"

func calculateHeaderFrame() -> CGRect {
    headerView.setNeedsLayout()
    headerView.layoutIfNeeded()

    let expectedHeaderSize = CGSize(width: view.bounds.width, height: headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height)

    return CGRect(origin: .zero, size: expectedHeaderSize)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    expectedSectionHeaderFrame = calculateHeaderFrame()
    return expectedSectionHeaderFrame.size
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    guard kind == UICollectionElementKindSectionHeader else { fatalError("Unexpected kind of supplementary view in (type(of: self))") }

    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath)
    headerView.frame = expectedSectionHeaderFrame

    return headerView
}
Run Code Online (Sandbox Code Playgroud)