Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI, ScrollView pushing VStack images up in ZStack

When I apply the scrollview it pushes my whole view up and leaves a white space below the bottom of the ZStack. Not sure why but if someone can please help.

var body: some View {


        ZStack{
            GeometryReader { geometry in
            Spacer()
             ScrollView (.vertical, showsIndicators: false) {
            VStack (spacing : self.Vspacing){
                Spacer()
                ForEach (self.buttons, id: \.self) { row in
                    SportsButtonsRow(screenWidth: geometry.size.width,
                                     Hspacing: self.Hspacing,
                                     buttons: row,  didTapButton: { sportButton in self.displayText = sportButton.title}
                                    )
                }//end foreach
               //  Spacer ()
            }.background(Color.black)//end of VStack
        }//end of scroll view
                 }//close geometry reader
    }//end of ZStack
        .edgesIgnoringSafeArea(.all)

}//end of main some View

}

View pushing up causing white space

like image 270
bain Avatar asked Feb 21 '26 10:02

bain


1 Answers

You need to change the order of containers construction. The following scratch demo works:

var body: some View {
        ZStack{
            GeometryReader { geometry in
                ScrollView (.vertical, showsIndicators: false) {

                // YOUR CONTENT HERE

                }
                .background(Color.black)//end of VStack
                .frame(height: geometry.size.height)
                //end of scroll view
        } //close geometry reader
        .edgesIgnoringSafeArea(.all)
    } //end of ZStack
} //end of main some View
like image 140
Asperi Avatar answered Feb 25 '26 01:02

Asperi