【问题标题】:UICollectionView AutoSize header heightUICollectionView AutoSize 标头高度
【发布时间】: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


    【解决方案1】:

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

    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
    }
    

    【讨论】:

    • 您究竟在哪里实施 calculateHeaderFrame ? headerView 变量在哪里定义? headerView 是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    相关资源
    最近更新 更多