我真的很接近在SwiftUI中为UITextView实现动态高度。请帮我解决这些问题:
[SwiftUI] Modifying state during view update, this will cause undefined behavior.
这是我的代码:
ItemEditView
TextView(text: $notes, textViewHeight: $textViewHeight)
.frame(height: self.textViewHeight)
UITextView
import SwiftUI
struct TextView: UIViewRepresentable {
@Binding var text: String
@Binding var textViewHeight: CGFloat
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.delegate = context.coordinator
textView.font = .systemFont(ofSize: 17)
textView.backgroundColor = .clear
return textView
}
func updateUIView(_ textView: UITextView, context: Context) {
textView.text = text
}
class Coordinator: NSObject, UITextViewDelegate {
var control: TextView
init(_ control: TextView) {
self.control = control
}
func textViewDidChange(_ textView: UITextView) {
control.text = textView.text
}
}
func makeCoordinator() -> TextView.Coordinator {
Coordinator(self)
}
}
有人问过类似的问题,但是没有一种解决方案对我有用。