Giles
Giles

Reputation: 1668

SwiftUI TextEditor expands incorrectly in a VStack with maxWidth

I have two TextEditors in a VStack in my app. I would like each of them to expand to fit the text they contain, but remain hugging the text. Unfortunately, long text in the first TextEditor expands both TextEditors equally. It is as if the total height of the VStack they are in is expanded correctly, but the extra height is shared between the two editors.

This only occurs when I use a maxWidth on the VStack. If I fix the width it behaves correctly.

Is there some way to solve this without losing the resize behaviour maxWidth gives?

Initial text locations

With expanded text

ScrollView {
    VStack(alignment: .leading) {
        TextEditor(text: Binding($note.details)!)
            .font(.body)

        TextEditor(text: Binding($note.title)!)
            .font(.title)

    }.frame(maxWidth: 450)
}

Upvotes: 0

Views: 505

Answers (1)

Over Ukraine
Over Ukraine

Reputation: 326

Try to put .fixedSize(horizontal: false, vertical: true) on both editors.

Upvotes: 4

Related Questions