Search code examples
iosswiftuivstack

Why is there a Gap between VStacks in SwiftUI?


A whiteness is seen in the area drawn with the red line. If I change the background color of the most inclusive Vstack, that white area changes.

Deleting spacer() lines doesn't work.

Why is there a gap even though there is no space in between?

struct TabbarView: View {    
var body: some View {        
    VStack{
        Spacer()   
            ZStack{
                Color.orange.opacity(0.5)
                VStack(spacing: 0){
                    Text("Home")
                        .padding()
                }
            }            
        Spacer()            
        HStack{
            VStack{
                Image(systemName: "homekit")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: UIScreen.main.bounds.size.width / 15, height: UIScreen.main.bounds.size.height / 25)
            }
        }
        .frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height / 13)
        .background(Color.purple)            
    }
    .ignoresSafeArea()      
   // .background(Color.purple.shadow(radius: 2))
}

}

enter image description here


Solution

  • you can add for VStack:

     VStack {}
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
    

    updated:

    struct ContentView: View {
        var body: some View {
            ZStack {
                Color.orange.opacity(0.5)
                VStack {
                    Spacer()
                    VStack(spacing: 0){
                        Text("Home")
                            .padding()
                    }
                    Spacer()
                    HStack {
                        VStack {
                            Image(systemName: "homekit")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 25, height: 25, alignment: .center)
                                .frame(width: UIScreen.main.bounds.size.width / 15, height: UIScreen.main.bounds.size.height / 25)
                        }
                    }
                    .frame(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height / 13)
                    .background(Color.purple)
                }
                
            }
            .ignoresSafeArea()
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
        }
    }
    

    you used nesting incorrectly and there is a native TabView for tabs

    result: enter image description here