如何在ScrollView中的SwiftUI中创建网格视图?

如何在ScrollView中的SwiftUI中创建网格视图?,swiftui,Swiftui,我需要在SwiftUI项目的ScrollView中创建一个简单的网格视图 基本上,我需要在scrollview中并排放置几个按钮(一行两个) 但是,当我编译我的应用程序时,按钮是在彼此下面的 我需要创建如下内容: 这是我的代码: ScrollView(.vertical){ Spacer(minLength: 10) Button(action: {

我需要在SwiftUI项目的ScrollView中创建一个简单的网格视图

基本上,我需要在scrollview中并排放置几个按钮(一行两个)

但是,当我编译我的应用程序时,按钮是在彼此下面的

我需要创建如下内容:

这是我的代码:

ScrollView(.vertical){
      
                Spacer(minLength: 10)
                
                
                Button(action: { }) {
                    Image("mountain")
                        //declare your image as resizable, otherwise it will keep its original size
                        .resizable()
                        //declare the frame of the image (i.e. the size you want it to resize to)
                        .frame(width: 160, height: 200)
                        // place the red rectangle with text in an overlay
                        // as opposed to HStack where elements are side by side
                        // here the image will be placed under the rest
                        .overlay(
                            //This HStack places the text in the middle with some padding
                            VStack(spacing: 0) {
                               
                                Text("Learning to")
                                    .font(Font.custom("Nagietha-Regular", size: 40))
                                    .foregroundColor(.white)
                                   // .padding(.vertical)
                                
                                
                                
                                Text("Meditate")
                                    .font(.title)
                                    .foregroundColor(.white)
                                    .fontWeight(.heavy)
                                    //.padding(.vertical)
                             
              
                            }
                    
                                //set the background of the text to be semitransparent red
                                .background(Color.clear.opacity(0.6)),
                                //this defines the alignment of the overlay
                            alignment: .center)
                        //clip everything to get rounded corners
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                    }
                
                
                Button(action: { }) {
                    Image("mountain")
                        //declare your image as resizable, otherwise it will keep its original size
                        .resizable()
                        //declare the frame of the image (i.e. the size you want it to resize to)
                        .frame(width: 160, height: 200)
                        // place the red rectangle with text in an overlay
                        // as opposed to HStack where elements are side by side
                        // here the image will be placed under the rest
                        .overlay(
                            //This HStack places the text in the middle with some padding
                            VStack(spacing: 0) {
                               
                                Text("Learning to")
                                    .font(Font.custom("Nagietha-Regular", size: 40))
                                    .foregroundColor(.white)
                                   // .padding(.vertical)
                                
                                
                                
                                Text("Meditate")
                                    .font(.title)
                                    .foregroundColor(.white)
                                    .fontWeight(.heavy)
                                    //.padding(.vertical)
                             
              
                            }
                    
                                //set the background of the text to be semitransparent red
                                .background(Color.clear.opacity(0.6)),
                                //this defines the alignment of the overlay
                            alignment: .center)
                        //clip everything to get rounded corners
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                    }
  
              }

有人能给我一些建议吗?

没有懒散的网格还有什么办法吗?当然,把
HStack
s和一对按钮放在
VStack
行中。我相信你能想出更多的方法。您要求复制照片中的
视图。这可能是最简单和最灵活的。我尝试过HStack解决方案,但它会将按钮排成一长列,彼此相邻!这就是为什么a说a对。您可以将带对的
HStack
s放入
VStack
行中。
struct SimpleGridView: View {
    @State var yourArray: [Int] =  [1,2,3,4,5,6,7,8,9]
    var columns: [GridItem] =
        Array(repeating: .init(.flexible()), count: 2)
    @State var selection: Int = 1
    var body: some View {
        VStack{
            Picker(selection: $selection, label: Text("GridStyles"), content: {
                Text("Easily grows").tag(1)
                Text("manual").tag(2)
            }).pickerStyle(SegmentedPickerStyle())
            
            switch selection {
            case 1:
                ScrollView{
                    LazyVGrid(columns: columns){
                        //This doesnt have to be a loop but it would make it scalable
                        ForEach(yourArray, id: \.self){ item in
                            Button(action: {
                                print(item.description)
                            }, label: {
                                Rectangle().overlay(Text(item.description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                                
                            })
                        }
                    }
                }
            case 2:
                VStack{
                    HStack{
                        Button(action: {
                            print(yourArray[0].description)
                        }, label: {
                            Rectangle().overlay(Text(yourArray[0].description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            
                        })
                        Button(action: {
                            print(yourArray[1].description)
                        }, label: {
                            Rectangle().overlay(Text(yourArray[1].description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            
                        })
                    }
                    HStack{
                        Button(action: {
                            print(yourArray[2].description)
                        }, label: {
                            Rectangle().overlay(Text(yourArray[2].description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            
                        })
                        Button(action: {
                            print(yourArray[3].description)
                        }, label: {
                            Rectangle().overlay(Text(yourArray[3].description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            
                        })
                    }
                    HStack{
                        Button(action: {
                            print(yourArray[4].description)
                        }, label: {
                            Rectangle().overlay(Text(yourArray[4].description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            
                        })
                        Button(action: {
                            print(yourArray[5].description)
                        }, label: {
                            Rectangle().overlay(Text(yourArray[5].description).foregroundColor(.white)).foregroundColor(.blue).frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealWidth: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, minHeight: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, idealHeight: /*@START_MENU_TOKEN@*/100/*@END_MENU_TOKEN@*/, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                            
                        })
                    }
                }
                
            default:
                Text("unknown selection")
            }
        }
    }
}