rawbee
rawbee

Reputation: 3008

In SwiftUI, why does a Spacer push the text outside of the TextEditor frame?

In the following code, why does the frame of the TextEditor stay within the safe area, while the text is pushed up to the top of the screen, beyond the safe area, and outside of the TextEditor's frame?

VStack {
    TextEditor(text: $myText)
        .frame(height: 200)
        .border(Color.red)
    Spacer()
}

enter image description here

Upvotes: 3

Views: 897

Answers (1)

Raja Kishan
Raja Kishan

Reputation: 18904

You have to use .padding()to adjust the text frame.

Adding padding is like adding textContainerInset in UITextView.

Like this

struct TestFrameView: View {
    
    @State private var myText: String = "Hello"
    
    var body: some View {
        VStack {
            TextEditor(text: $myText)
                .padding() //<-- here or .padding(.top, 0.1) for zero space.
                .frame(height: 200)
                .border(Color.red)
            Spacer()
        }
    }
}

Upvotes: 4

Related Questions