NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

TextEditor bug help.

Forums > SwiftUI

Hi All,

I have a bit of a strange or maybe even not so strange bug with making a dynamically expanding TextEditor feild in SwiftUI and I was wondering if anyone can help.

Basically, I have found a way of making a TextEditor feild look similar to a TextField as in a single line to start and then as you type it dynaically grows to any size needed using the following code:

TextEditor(text: $textBox)
            //.frame(height: 40) // Keeps constraint
            //.frame(minHeight: 40) // Causes to rise?
            //.frame(maxHeight: 200) // Not used for testing
            .frame(minHeight: 40, maxHeight: 500, alignment: .leading)
            //.padding(.leading , 8)
            .fixedSize(horizontal: false, vertical: true) // Keeps contained
            .overlay {
                RoundedRectangle(cornerRadius: 20)
                .stroke(Color.gray, lineWidth: 2) }

However, a bug has appeared that I am unable to work out. Basically, if you type or even paste content into the TextEditor made above all works fine and the TextEditor dynamically expands... great... this is just what I want, however if you hit the enter/return key at any point the TextEditor box shrinks and the contents get obscured until it then grows back to the maxHeight set when you type more. Can anybody help me work out what’s going on please? I have a feeling the minHeight property gets re-invoked when the enter key is pressed but I'm not able to find a way to text this as if I just set the height I get a regular single line TextEditor.

Thanks in advance!


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.