What is difference between GeometryReader and GeometryProxy in SwiftUI?

Viewed 3645

As per Apple,

GeometryReader A container view that defines its content as a function of its own size and coordinate space.

GeometryProxy: A proxy for access to the size and coordinate space (for anchor resolution) of the container view.

I am trying to understand when to use GeometryReader and when to use GeometryProxy? I did google but didn't see any post coming up in results. So I am asking here so that new developers like me can use it for reference.

1 Answers

GeometryReader

SwiftUI’s GeometryReader allows us to determine the size and coordinates of views as a function of its own size and coordinates.

You can use a GeometryReader like this:

GeometryReader { geometry in
    SomeView()
        .offset(x: geometry.size.width / 2)
}

GeometryProxy

The closure variable (geometry) in the code above is of type GeometryProxy. This struct provides us with the following information:

public var size: CGSize { get }
public var safeAreaInsets: EdgeInsets { get }
public func frame(in coordinateSpace: CoordinateSpace) -> CGRect
public subscript<T>(anchor: Anchor<T>) -> T where T : Equatable { get }

Basically a GeometryReader reads the view (its size, coordinates etc.) and returns a GeometryProxy struct from which you can access all the information.

Useful links:

Related