SwiftUI,ScrollView将ZStack中的VStack图像向上推

时间:2019-12-19 03:50:28

标签: scrollview swiftui vstack

当我应用scrollview时,它将向上推整个视图,并在ZStack底部下方留出空白。不知道为什么,但是如果有人可以请帮助。

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

2 个答案:

答案 0 :(得分:0)

您需要更改容器构造的顺序。以下临时演示作品有效:

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

答案 1 :(得分:0)

我知道了。看来您需要为scrollview指定帧大小,所以我这样做了。

       GeometryReader { geometry in
            Spacer()
                VStack{
                    Text ("fdklafdfa")
                        .foregroundColor(.blue)
                         .frame(width: geometry.size.width, height:geometry.size.height * 0.32 , alignment: .bottom)
                    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 ()
                             }//end of VStack
                         }//end of scroll view
                        .frame(width: geometry.size.width, height:geometry.size.height * 0.68 , alignment: .bottom)
                        .edgesIgnoringSafeArea(.all)
                        .background(Color.black)
                }//End of main VStack

                 }//close geometry reader