Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between GeometryReader and GeometryProxy in SwiftUI?

Tags:

swiftui

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.

like image 807
Naren Avatar asked Dec 23 '22 17:12

Naren


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:

  • Understanding frames and coordinates inside GeometryReader
  • GeometryReader to the Rescue
  • Anchor preferences in SwiftUI
like image 122
pawello2222 Avatar answered Jan 19 '23 01:01

pawello2222