Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 根据行数动态更改文本视图高度_Ios_Swift - Fatal编程技术网

Ios 根据行数动态更改文本视图高度

Ios 根据行数动态更改文本视图高度,ios,swift,Ios,Swift,我正在根据文本视图中的行数动态更改文本视图的高度。我希望文本视图在第6行之后开始增加高度。我当前的实现导致文本视图的高度不断增加,因为行数始终大于上一个计数。我还希望文本视图在删除行时缩小: func textViewDidChange(_ textView: UITextView) { let lineCount = numberOfLines(textView: textView) if lineCount > previousLineCount {

我正在根据文本视图中的行数动态更改文本视图的高度。我希望文本视图在第6行之后开始增加高度。我当前的实现导致文本视图的高度不断增加,因为行数始终大于上一个计数。我还希望文本视图在删除行时缩小:

func textViewDidChange(_ textView: UITextView) {

   let lineCount = numberOfLines(textView: textView)

      if lineCount > previousLineCount {
            previousLineCount = lineCount - 1
      }

      print("previous number of lines: \(previousLineCount) current number of lines: \(lineCount)" )

      if lineCount > 6 && lineCount > previousLineCount {
           self.layoutConstraint.constant += 4
      }
}

有什么想法吗?

考虑到您可以添加前导、尾随、顶部和链接高度约束

func textViewDidChange(_ textView: UITextView) { 
   self.heightConstraint.constant = textView.contentSize.height
   self.view.layoutIfNeeded()
}
为什么不:

textView.sizeToFit()
不要忘记在情节提要中禁用滚动,或者:

textView.scrollEnabled = false