【发布时间】:2019-05-15 17:11:30
【问题描述】:
我将 UITextView 放在 UIView 中。 UIView 会随着用户在 UITextView 中键入而扩展,但问题是如果用户在下一行键入,它不会显示正在键入的文本,直到用户在第三行键入,然后它会显示打印在第二行。第 3 行和第 4 行等也是如此。
我该如何解决这个问题?
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: prayerRequest.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraints) in
if constraints.firstAttribute == .height {
constraints.constant = estimatedSize.height
}
viewContainer.constraints.forEach({ (constraints) in
if constraints.firstAttribute == .height {
constraints.constant = estimatedSize.height
viewContainer.layoutIfNeeded()
}
})
}
}
【问题讨论】:
-
试试
view.layoutIfNeeded()而不是viewContainer.layoutIfNeeded() -
@Sh_Khan 现在试过了,它仍然做同样的事情:(
标签: ios swift uitextview