Using GeometryReader with ScrollView

て烟熏妆下的殇ゞ 提交于 2021-01-28 13:50:03

问题


I'm trying to use GeometryReader to assign a maxHeight to a list in a Scrollview.
For the purpose of this question, I created the following project : GeometryReaderTesting.

It sets up a ContentView with a :
- Text.
- List.
- Text.
I extracted the List and last Text into their own view, using @ViewBuilder and I want to set the maxHeight of the List to .5 the height of my user's screen.

The problem is that the app won't build with the following errors, and the GeometryReader doesn't seem to be computing the correct height:

Here is my ContentView code, if anyone has an idea of what I'm doing wrong...

struct ContentView: View {

    let arrayWithStuff = [ "one","two","three","four","five","six","seven","eight","nine","ten", "eleven", "twelve"]

    var heightOfView: CGFloat = 0.0 // To be set by GeometryReader

    var body: some View {

    let myString = "Top of Test App"

     return   ZStack {
            GeometryReader { g in
                heightOfView = g.size.height
        NavigationView {

            print("height of view : \(heightOfView)")
             ScrollView {
                VStack {
                    Text(myString)
                    .padding()
                    Divider()
                    self.ViewBody()
                } // END of Vstack
                    .navigationBarTitle("Test App", displayMode: .inline)
            } // END of Scrollview
                }//End of NavigationView
            } // End of Geometry reader
        } // End of Zstack

  } // End of body


    @ViewBuilder func ViewBody() -> some View {
        VStack {
            List {
                ForEach (self.arrayWithStuff, id: \.self) { item in
                    Text(item) }
            } // END of List
                .frame(maxHeight: heightOfView*0.5)
            Divider()
            Text("Bottom of TEST APP")
        }
        .padding()
    }
}

Again, any help would be appreciated.


回答1:


Here is a demo of possible solution. Tested with Xcode 11.4.

struct ContentView: View {

    let arrayWithStuff = [ "one","two","three","four","five","six","seven","eight","nine","ten", "eleven", "twelve"]

    var body: some View {

        let myString = "Top of Test App"

        return   ZStack {
            GeometryReader { g in
                NavigationView {
                    ScrollView {
                        VStack {
                            Text(myString)
                                .padding()
                            Divider()
                            self.ViewBody(height: g.size.height)
                        } // END of Vstack
                            .navigationBarTitle("Test App", displayMode: .inline)
                    } // END of Scrollview
                }//End of NavigationView
            } // End of Geometry reader
        } // End of Zstack

    } // End of body


    func ViewBody(height: CGFloat) -> some View {
        print("height of view : \(height)")
        return VStack {
            List {
                ForEach (self.arrayWithStuff, id: \.self) { item in
                    Text(item) }
            } // END of List
                .frame(height: height*0.5)
            Divider()
            Text("Bottom of TEST APP")
        }
        .padding()
    }
}


来源:https://stackoverflow.com/questions/62061420/using-geometryreader-with-scrollview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!