0

我正在使用包装的UITextViewvia UIViewRepresentable。根据其内容,即(属性)字符串,该 textView 应该与所需的一样高。

struct TextView: UIViewRepresentable {
    @Binding var text: String

    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.isScrollEnabled = false
        textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
    }
}

这工作正常:

struct TextView_Previews: PreviewProvider {
    static var previews: some View {
        TextView(text: .constant("some multiline\na\nb\nc\nd\nlorem ipsum"))
    }
}

不工作

struct TextView_Previews: PreviewProvider {
    static var previews: some View {
        ScrollView { // <- when placed inside ScrollView the height is reduced to one line
            TextView(text: .constant("some multiline\na\nb\nc\nd\nlorem ipsum"))
        }
    }
}

如何解决这个问题?

4

1 回答 1

0
struct TextView_Previews: PreviewProvider {
    static var previews: some View {
        ScrollView { // <- when placed inside ScrollView the height is reduced to one line
            TextView(text: .constant("some multiline\na\nb\nc\nd\nlorem ipsum"))
                    .frame(minHeight: 100)

        }
    }
}
于 2020-08-06T05:05:33.587 回答