Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在SwiftUI中使用GeometryReader定位视图_Ios_Swift_Swiftui - Fatal编程技术网

Ios 在SwiftUI中使用GeometryReader定位视图

Ios 在SwiftUI中使用GeometryReader定位视图,ios,swift,swiftui,Ios,Swift,Swiftui,我在scrollview中有一组子视图(GridView1和GridView2)。子视图在VStack中,因为我需要一个在另一个下面。我有一个GeometryReader包装子视图,每个子视图中都有一个GeometryReader。我需要子视图中的GeometryReader来确定LazyVGrid中需要的列间距的宽度 问题是,在子视图中使用GeometryReader会导致两个子视图堆叠在一起。我曾尝试设置子视图的帧大小,但这限制了垂直滚动,并没有给我正确的结果 如果您能帮我解决这个问题,我将

我在scrollview中有一组子视图(GridView1和GridView2)。子视图在VStack中,因为我需要一个在另一个下面。我有一个GeometryReader包装子视图,每个子视图中都有一个GeometryReader。我需要子视图中的GeometryReader来确定LazyVGrid中需要的列间距的宽度

问题是,在子视图中使用GeometryReader会导致两个子视图堆叠在一起。我曾尝试设置子视图的帧大小,但这限制了垂直滚动,并没有给我正确的结果

如果您能帮我解决这个问题,我将不胜感激

ContentView:

struct ContentView: View {
    var body: some View {
        GeometryReader { geo in
            ScrollView {
                VStack(spacing: 20) {
                    GridView1()
                    GridView2()
                }//.frame(minHeight: 0, maxHeight: .infinity)
            }
        }
        .padding()
    }
}
GridView1:

struct GridView1: View {
    var body: some View {
        GeometryReader { g in
            let maxwidth = g.size.width/6 > 100 ? g.size.width/6 : 100
            let columns = Array(repeating: GridItem(.flexible(minimum: 100, maximum: maxwidth), spacing: 0), count: 6)
            
            ScrollView(.horizontal) {
                LazyVGrid(columns: columns, alignment: .leading, spacing: 10, pinnedViews: [.sectionHeaders]) {
                    Section(header: Text("Grid 1").font(.title)) {
                        ForEach(0...200, id:\.self) { index in
                            Text("\(index)").frame(maxWidth: .infinity)
                        }
                    }
                }
            }
            .background(Color.blue)
        }
    }
}
GridView2(GridView1和GridView2本质上是相同的)

以下是我所期待的:

这是我目前得到的。如下图所示,子视图聚集在顶部


不要使用内部
GeometryReader
来混淆外部
ScrollView
,而是将宽度从顶部
GeometryReader
传递到子视图网格中

这是一个有效的解决方案。使用Xcode 12.1/iOS 14.1进行测试


这管用!非常感谢。但是,我想知道使用GeometryReader的正确方法是什么,因为这限制了GeometryReader在下游视图中的使用。你能给我指的任何指针或例子。谢谢
struct GridView2: View {
    var body: some View {
        GeometryReader { g in
            let maxwidth = g.size.width/10 > 100 ? g.size.width/10 : 100
            let columns = Array(repeating: GridItem(.flexible(minimum: 100, maximum: maxwidth), spacing: 0), count: 10)
            ScrollView(.horizontal) {
                LazyVGrid(columns: columns, alignment: .leading, spacing: 20, pinnedViews: [.sectionHeaders]) {
                    Section(header: Text("Grid 2").font(.title)) {
                        ForEach(1000...1200, id:\.self) { index in
                            ZStack {
                                Text("\(index)")
                            }
                            .frame(maxWidth: .infinity)
                            .background(Color.green)
                        }
                    }//.frame(minHeight: 0, maxHeight: .infinity)
                }
            }//.frame(minHeight: 1000, maxHeight: .infinity)
            //.frame(maxWidth: .infinity, maxHeight: .infinity)
            //.background(Color.red)
        }
    }
}
struct ContentView: View {
    var body: some View {
        GeometryReader { geo in
            ScrollView {
                VStack(spacing: 20) {
                    GridView1(width: geo.size.width)
                    GridView2(width: geo.size.width)
                }
            }
        }
        .padding()
    }
}

struct GridView1: View {
    let width: CGFloat
    var body: some View {
        let maxwidth = width/6 > 100 ? width/6 : 100
        let columns = Array(repeating: GridItem(.flexible(minimum: 100, maximum: maxwidth), spacing: 0), count: 6)
        
        ScrollView(.horizontal) {
            LazyVGrid(columns: columns, alignment: .leading, spacing: 10, pinnedViews: [.sectionHeaders]) {
                Section(header: Text("Grid 1").font(.title)) {
                    ForEach(0...200, id:\.self) { index in
                        Text("\(index)").frame(maxWidth: .infinity)
                    }
                }
            }
        }
        .background(Color.blue)
    }
}

struct GridView2: View {
    let width: CGFloat
    var body: some View {
        let maxwidth = width/10 > 100 ? width/10 : 100
        let columns = Array(repeating: GridItem(.flexible(minimum: 100, maximum: maxwidth), spacing: 0), count: 10)
        ScrollView(.horizontal) {
            LazyVGrid(columns: columns, alignment: .leading, spacing: 20, pinnedViews: [.sectionHeaders]) {
                Section(header: Text("Grid 2").font(.title)) {
                    ForEach(1000...1200, id:\.self) { index in
                        ZStack {
                            Text("\(index)")
                        }
                        .frame(maxWidth: .infinity)
                        .background(Color.green)
                    }
                }
            }
        }
    }
}