根据标题UITextview内容更新UICollectionView标题高度

时间:2018-09-28 12:17:11

标签: ios uicollectionview uitextview uicollectionviewcell

我有一个<Route path="/articles" render={() => <PageArticles width={200} />} /> 标题单元格,用于显示企业信息。

一家企业可能没有描述。

在标题单元格中,我设置了一个业务对象,然后为标签等设置了所有文本。在这里,我还尝试根据此post

计算所需的文本字段高度。

我需要的是在class event(): def __init__(self, description, priority): self.description = description self.priority = priority self.severity = priority pass _translateMap = { 'description': 'message', 'priority' : 'priority', 'severity': 'severity' } def translate(self): result = {} for key, value in self._translateMap.items(): result[key] = getattr(self, key) return result if __name__ == '__main__': event1 = event('blahblah','3') print (event1.translate()) 所在的位置实际动态设置标题单元格的高度。

UICollectionViewController类:

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

    }

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

1 个答案:

答案 0 :(得分:0)

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