【问题标题】:Update UICollectionView header height based on headers UITextview content根据标题 UITextview 内容更新 UICollectionView 标题高度
【发布时间】:2019-03-04 10:34:38
【问题描述】:

我有一个 UICollectionViewController 标题单元格,用于显示企业信息。

企业可能有也可能没有描述。

在我的标题单元格中,我设置了一个业务对象,然后设置了标签等的所有文本。在这里,我还尝试根据 post 计算所需的文本字段高度

我需要在使用它的UICollectionViewController 中动态设置我的标题单元格高度。

BusinessDetailHeader类:

    var maxTextViewHeight: CGFloat = 0

    var business: Business? {
        didSet {
            guard let imageUrl = business?.logoUrl else {return}

            let url = URL(string: imageUrl)
            logoView.kf.setImage(with: url)
            headerImageView.kf.setImage(with: url)

            guard let businessName = business?.name else {return}
            guard let address = business?.address else {return}
            guard let hoursOfOperation = business?.hoursOfOperation else {return}
            guard let phoneNumber = business?.phoneNumber else {return}

            businessNameLabel.text = businessName
            addressLabel.text = address
            hoursofOperationLabel.text = hoursOfOperation
            phoneNumberLabel.text = phoneNumber

            if let description = business?.description {
                detailsTextField.text = description
                let amountOfLinesToBeShown:CGFloat = 6
                if let height = detailsTextField.font?.lineHeight {
                    maxTextViewHeight = height * amountOfLinesToBeShown
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutViews()
    }

    fileprivate func layoutViews() {
        addSubview(headerImageView)
        headerImageView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 130)

        //ADJUST DETAIL HEIGHT HERE BASED ON CALCULATED HEIGHT FOR TEXTVIEW
        addSubview(detailView)
        detailView.anchor(top: headerImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 200+maxTextViewHeight)

        detailView.addSubview(businessNameLabel)
        businessNameLabel.anchor(top: logoView.bottomAnchor, left: nil, bottom: nil, right: nil, paddingTop: 4, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        businessNameLabel.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true

        detailView.addSubview(detailsTextField)
        detailsTextField.anchor(top: businessNameLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: maxTextViewHeight)


        detailView.addSubview(addressLabel)
        addressLabel.anchor(top: detailsTextField.bottomAnchor, left: nil, bottom: nil, right: nil, paddingTop: 4, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        addressLabel.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true

        detailView.addSubview(hoursofOperationLabel)
        hoursofOperationLabel.anchor(top: addressLabel.bottomAnchor, left: nil, bottom: nil, right: nil, paddingTop: 4, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        hoursofOperationLabel.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true

        detailView.addSubview(phoneNumberLabel)
        phoneNumberLabel.anchor(top: hoursofOperationLabel.bottomAnchor, left: nil, bottom: nil, right: nil, paddingTop: 4, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        phoneNumberLabel.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true

    }

高度正确更新没有问题,但有时当高度太大时,文本和所有以下元素都会溢出标题单元格。我认为这与我在UICollecitonViewController 中处理单元格高度的方式有关。
我这样设置它的高度:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    return CGSize(width: view.frame.width, height: 330)
}

这里330的高度需要是动态的。这可能吗?我应该像这样设置高度属性吗?我见过像one 这样的帖子尝试解决这个问题,但解决方案似乎并不是最好的。

【问题讨论】:

标签: ios uicollectionview uitextview uicollectionviewcell


【解决方案1】:

请检查此链接,它对如何make UIcollectionView header dynamic height? 非常有帮助。基本上你需要根据你的要求设置自动布局约束;需要在referenceSizeForHeaderInSection(collectionViewFlowLayout delegate)方法中调用setNeedlayout()。

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多