Search code examples
iosswiftiphoneswiftui

Empty View taken space


I want to show EmptyView() without space if there is no data from sever.

If I put my code inside VStack then EmptyView() takes the space, If I put the code without Stack, then API is calling infinite number.

My code is follow:

VStack{
    if let arr = vm.arrMessages{
        getContent()
            .padding(.top)
    } else if vm.isLoading {
        ProgressView()
    } else if vm.isError {
        EmptyView()
    }
}

// Outside of VStack:

if let arr = vm.arrMessages{
    getContent()
        .padding(.top)
} else if vm.isLoading {
    ProgressView()
} else if vm.isError {
    EmptyView()
}

then API is calling infinite time.

thank you


Solution

  • When you are inside the VStack, you can simply don't put anything to get rid of undesired spacing:

    VStack {
        if let arr = vm.arrMessages{
            getContent()
                .padding(.top)
        } else if vm.isLoading {
            ProgressView()
        } else if vm.isError {
            /* Nothing Here */ // 👈 Return nothing
        }
    }
    

    You can also set a custom spacing like 0 for the Stack like:

    VStack(spacing: 0) { // 👈 The default value is dynamic value called `standard` which most of the time appears around 8 points
        ,,,
    }