Как получить динамическую c высоту для UITextView в SwiftUI - PullRequest
0 голосов
/ 18 марта 2020

Я очень близок к реализации динамической c высоты для UITextView в SwiftUI. Пожалуйста, помогите мне разобраться с этими изломами:

  1. UITextView имеет правильную высоту, когда он появляется, но не регулирует высоту во время редактирования; Я хотел бы настроить его.
  2. Я получаю это в консоли каждый раз, когда редактирую текст в TextView: [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)
    }
}

Аналогичные вопросы были заданы, но ни одно из решений не помогло мне.

...