我有这个UITextView&我希望它的高度在用户在其上键入时动态变化。我想以编程方式进行操作。我在另一个UIView上方有UITextView。约束设置如下:

 addtextview.leadingAnchor.constraint(equalTo: addtasktextview.leadingAnchor, constant: 8).isActive = true
      addtextview.trailingAnchor.constraint(equalTo: addtasktextview.trailingAnchor, constant: -8).isActive = true
      addtextview.topAnchor.constraint(equalTo: addtasktextview.topAnchor, constant: 8).isActive = true
      addtextview.bottomAnchor.constraint(equalTo: addtasktextview.bottomAnchor, constant: -40).isActive = true

有趣的是,我也使用tableViewCells中的textview并仅使用此约束方法来动态更改高度,但是在这里它不起作用。
我希望textview的高度以向上移动的方式增加。因此,当新行开始时,顶部应移动并保持下面的间距。

我该怎么做?帮助将不胜感激。

ios - UITextView的高度可以在键入/不使用 Storyboard 时动态更改-LMLPHP

更新:我能够使其与@ upholder-of-truth的下面的答案一起使用。通过找到textview正常高度和newSize.height之间的差异,然后将该差异添加到容器的高度中,我还能够动态更改父UIView容器的高度。

最佳答案

首先,请确保您的类采用UITextViewDelegate协议(protocol),以便在文本更改如下时得到通知:

class MyClass: UIViewContoller, UITextViewDelegate

接下来,在您的类中的某个位置定义此变量,以便您可以跟踪约束中的高度:
var textHeightConstraint: NSLayoutConstraint!

接下来添加以下约束并激活它:
    self.textHeightConstraint = addtextview.heightAnchor.constraint(equalToConstant: 40)
    self.textHeightConstraint.isActive = true

(如果您不在viewDidLoad中执行此操作,则需要将textHeightConstraint设为可选)

接下来订阅委托(delegate)(如果尚未完成):
addTextView.delegate = self

添加此函数以重新计算高度约束:
func adjustTextViewHeight() {
    let fixedWidth = addtextview.frame.size.width
    let newSize = addtextview.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    self.textHeightConstraint.constant = newSize.height
    self.view.layoutIfNeeded()
}

在创建约束以设置初始大小之后,接下来添加对该函数的调用:
self.adjustTextViewHeight()

最后,添加此方法以在文本更改时调整高度:
func textViewDidChange(_ textView: UITextView) {
    self.adjustTextViewHeight()
}

以防万一,这是一个UIViewController子类中的一个最小示例:
class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet var textView: UITextView!
    @IBOutlet var textHolder: UIView!

    var textHeightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textView.leadingAnchor.constraint(equalTo: textHolder.leadingAnchor, constant: 8).isActive = true
        textView.trailingAnchor.constraint(equalTo: textHolder.trailingAnchor, constant: -8).isActive = true
        textView.topAnchor.constraint(equalTo: textHolder.topAnchor, constant: 8).isActive = true
        textView.bottomAnchor.constraint(equalTo: textHolder.bottomAnchor, constant: -40).isActive = true

        self.textHeightConstraint = textView.heightAnchor.constraint(equalToConstant: 40)
        self.textHeightConstraint.isActive = true

        self.adjustTextViewHeight()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textViewDidChange(_ textView: UITextView) {
        self.adjustTextViewHeight()
    }

    func adjustTextViewHeight() {
        let fixedWidth = textView.frame.size.width
        let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
        self.textHeightConstraint.constant = newSize.height
        self.view.layoutIfNeeded()
    }
}

10-07 19:35
查看更多