通过更改多个文本视图的高度来动态更改父视图的高度

时间:2018-12-13 02:11:47

标签: swift xcode textview

我在容器视图中有三个textview。当用户在每个textView中键入多行时,我需要更改父容器View的高度。当我在父视图中仅使用一个textView时,我发现此link很有用。我已经以编程方式编写了约束

func adjustTextViewHeight(textView: UITextView) {
    let fixedWidth = textView.frame.size.width
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    let difference = newSize.height - 32

    if textView == titleTextView{
        self.titleHeightConstraint?.constant = newSize.height
    }else if textView == ingredientsTextView{
        self.ingredientsHeightConstraint?.constant = newSize.height
    }else{
        self.directionsHeightConstraint?.constant = newSize.height
    }

    self.containerHeightConstraint?.constant = 280 + difference

    scrollContent = 812 + difference
    self.view.layoutIfNeeded()
}

func textViewDidChange(_ textView: UITextView) {
    if textView == titleTextView{
        adjustTextViewHeight(textView: titleTextView)
    }else if textView == ingredientsTextView{
        adjustTextViewHeight(textView: ingredientsTextView)
    }else if textView == directionTextView{
        adjustTextViewHeight(textView: directionTextView)
    }
}

对于这三种文本视图,该代码似乎不起作用。我在哪里做错了?请帮忙。

0 个答案:

没有答案