Enumeration¶
CoordinateSpace¶
Represents the context (or space) of a set of coordinates relative to the screen.¶
Declaration¶
enum CoordinateSpace
Overview¶
CoordinateSpace allows a set of X, Y coordinates to have context on if they are relative to the frame's parent, or absolute to the device screen. It is also possible to define a custom coordinate space on a view with coordinateSpace(name:). This custom coordinate space can be checked with the .named(AnyHashable) case.
For example:
struct ContentView: View {
var body: some View {
HStack {
Rectangle()
.fill(Color.red)
GeometryReader { geometryProxy in
Button(action: {
let globalFrame = geometryProxy.frame(in: CoordinateSpace.global)
let localFrame = geometryProxy.frame(in: CoordinateSpace.local)
print("Global center: \(globalFrame.midX) x \(globalFrame.midY)") //"Global center: 160.0 x 294.0"
print("Local center: \(localFrame.midX) x \(localFrame.midY)") //"50.67 x 274.0"
}, label: {
Text("Button with coordinate spaces")
})
.background(Color.orange)
}
Rectangle()
.fill(Color.red)
}
}
}

In this example, the center of the Button coordinates is retrieved. The global center means that the middle of the button is 160 pixels from the left of the screen, and 294 pixels from the top of the screen. The local center means that the middle of the button is ~50 pixels from the left edge of it's container (which is the GeometryReader) and the middle of the button is 274 pixels from the top of it's container.
There are two additional important things to note about this example:
- For illustrative purposes, the enum values were expanded in this example. It would also be valid to use the shorthand, just specifying the values: .global or .local.
- The exact output of coordinates will differ depending on screen size, but the local midX coordinate will always be smaller than the global midX since it's relative.
Availability¶
iOS 13.0+
macOS 10.15+
tvOS 13.0+
watchOS 6.0+
Topics¶
Type Method¶
==(lhs:rhs:) Returns a Boolean value indicating whether two values are equal.
Instance Method¶
hash(into:) Hashes the essential components of this value by feeding them into the given hasher.
Instance Property¶
hashValue The hash value.
isGlobal Whether or not the coordinate space is CoordinateSpace.global
isLocal Whether or not the coordinate space is CoordinateSpace.local
Case¶
global The absolute coordinate space representing the entire screen.
local The relative coordinate space representing the current view.
named A coordinate space chosen by the Hashable parameter.