user16856881
user16856881

Reputation:

binding the GeometryReader in swiftui

how to binding the GeometryReader 's item in childview? i use below code but no work? but it show

Cannot call value of non-function type 'GeometryProxy'

is there any value to binding it?

GeometryReader { geometry in
geometry(geometry: geometry}

}
struct geometry: View {
    @Binding var geometry:GeometryProxy
    var body: some View {
                Circle()
                    .foregroundColor(.blue).opacity(0.5).blur(radius: 5)
                    .frame(width: geometry.size.width * 0.04, height: geometry.size.width 
              * 0.04)
}
        
}

Upvotes: 0

Views: 1550

Answers (1)

George
George

Reputation: 30361

To make this easier to understand the solution, here is a minimal reproducible example of the problem:

struct ContentView: View {
    var body: some View {
        GeometryReader { geometry in
            geometry(geometry: geometry)
        }
    }
}

struct geometry: View {
    @Binding var geometry: GeometryProxy

    var body: some View {
        Circle()
            .foregroundColor(.blue)
            .opacity(0.5)
            .blur(radius: 5)
            .frame(width: geometry.size.width * 0.04, height: geometry.size.width * 0.04)
    }
}

There are 2 main issues:

  1. You have two geometry identifiers with different meanings.
  2. You cannot pass a Binding into the initializer.

#1

You have geometry which is a GeometryProxy and another geometry which is a View. The inner geometry (GeometryProxy) is preferred since it is in the most local scope. This means that you can't access the geometry view.

You can solve this by using a capital letter on the view, which you should do by convention anyway (to avoid problems like this):

struct Geometry: View {
    /* ... */
}

#2

Now that you have solved the first problem, you will now get the error:

Cannot convert value of type 'GeometryProxy' to expected argument type 'Binding'

Solve this by changing the line:

@Binding var geometry: GeometryProxy

To:

let geometry: GeometryProxy

Upvotes: 2

Related Questions