0

I am trying to get the height of an item im creating by setting a geometry reader, then adding an .opAppear on an empty ZStack which pulls the height of the geo reader.

struct MyView: View {
    @State private var height: CGFloat = 0

    var body: some View {
        GeometryReader { geometry in
            ZStack {/*empty ZStack*/}.onAppear {perform: self.height = geometry.size.height
            }
            //More code...
        }
    }
}

My first question is why do I get an error which says "cannot convert value type '()' to expected argument type '(() -> Void)?'" on the .onAppear

My second question would be is if there is a better item than a ZStack to use for this?

1 Answers1

0

Here are a few modifications which should help with the readability of your code. I'm not clear on your goal, but this will now compile.

import SwiftUI

struct MyView: View {
    @State private var height: CGSize = .zero

    var body: some View {
        GeometryReader { geometry in
            ZStack {
                
            }
            .onAppear {
                self.height = geometry.size
            }
        }
    }
}
mgrillo
  • 21
  • 1
  • 2