Joris
Joris

Reputation: 6286

How to reserve vertical space for Text in SwiftUI?

I have a VStack containing a Text and an Image. The text contents are loaded asynchronously.

The problem is that if the Text is empty it does not take up any space, and everything gets pushed down as soon as the Text has its string set. Is there a way to "reserve" this space?

e.g.

    var body: some View {
        VStack {
            Text($viewModel.loadedText)
            Image("myImage")
        }
    }

Upvotes: 2

Views: 910

Answers (1)

Ahmad
Ahmad

Reputation: 353

If the text is empty then just use a " " string. so now we have made sure that there always be some text in the Text view.

var body: some View {
    VStack {
        let text = viewModel.loadedText.isEmpty ? " " : viewModel.loadedText
        Text(text)
        Image("myImage")
    }
}

Upvotes: 1

Related Questions