如何在SwiftUI中获取UITextView的动态高度

如何在SwiftUI中获取UITextView的动态高度,swiftui,uiviewrepresentable,Swiftui,Uiviewrepresentable,我即将在SwiftUI中实现UITextView的动态高度。请帮我解决这些难题: UITextView显示时具有正确的高度,但在执行编辑时不调整高度;我想调整一下 每次在TextView中编辑文本时,我都会在控制台中收到此消息:[SwiftUI]在视图更新期间修改状态,这将导致未定义的行为。 这是我的密码: ItemEditView TextView(text: $notes, textViewHeight: $textViewHeight) .frame(height: self.te

我即将在SwiftUI中实现UITextView的动态高度。请帮我解决这些难题:

  • UITextView显示时具有正确的高度,但在执行编辑时不调整高度;我想调整一下
  • 每次在TextView中编辑文本时,我都会在控制台中收到此消息:
    [SwiftUI]在视图更新期间修改状态,这将导致未定义的行为。
  • 这是我的密码:

    ItemEditView

    TextView(text: $notes, textViewHeight: $textViewHeight)
        .frame(height: self.textViewHeight)
    
    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)
        }
    }
    
    UITextView

    TextView(text: $notes, textViewHeight: $textViewHeight)
        .frame(height: self.textViewHeight)
    
    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)
        }
    }
    

    有人问过类似的问题,但没有一个解决方案适合我。

    以下主题应该会有帮助,谢谢Asperi!我会对你的帖子发表评论,但我还没有得到足够的分数。我得到了以下错误:“self.\u showingPlaceholder=State(initialValue:self.text.isEmpty)”行中的“初始化前使用的变量”self.item“。有什么想法吗?在我的代码中没有self.item,所以我不知道。你是对的-那是我的错误。我在一个工作表中显示TextView,现在调用我的视图时出现了一个错误:“调用/插入'text:','中缺少参数'text'的参数”,有什么方法可以解决这个问题吗?我不想从该视图传入绑定。Asperi,您是否找到了添加/删除段落时插入符号跳到TextView结尾的解决方案?以下主题应该会有所帮助,谢谢Asperi!我会对你的帖子发表评论,但我还没有得到足够的分数。我得到了以下错误:“self.\u showingPlaceholder=State(initialValue:self.text.isEmpty)”行中的“初始化前使用的变量”self.item“。有什么想法吗?在我的代码中没有self.item,所以我不知道。你是对的-那是我的错误。我在一个工作表中显示TextView,现在调用我的视图时出现了一个错误:“调用/插入'text:','中缺少参数'text'的参数”,有什么方法可以解决这个问题吗?我不想从该视图传入绑定。Asperi,您是否找到了添加/删除段落时插入符号跳到TextView结尾的解决方案?