<!--
{
  "availability" : [
    "iOS: 13.0.0 - 27.0.0",
    "iPadOS: 13.0.0 - 27.0.0",
    "macCatalyst: 13.0.0 - 27.0.0",
    "macOS: 10.15.0 - 27.0.0",
    "tvOS: 13.0.0 - 27.0.0",
    "visionOS: 1.0.0 - 27.0.0",
    "watchOS: 6.0.0 - 27.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/coordinateSpace(name:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE15coordinateSpace4nameQrqd___tSHRd__lF"
  },
  "title" : "coordinateSpace(name:)"
}
-->

# coordinateSpace(name:)

Assigns a name to the view’s coordinate space, so other code can operate
on dimensions like points and sizes relative to the named space.

```
nonisolated func coordinateSpace<T>(name: T) -> some View where T : Hashable
```

## Parameters

`name`

A name used to identify this coordinate space.

## Discussion

Use `coordinateSpace(name:)` to allow another function to find and
operate on a view and operate on dimensions relative to that view.

The example below demonstrates how a nested view can find and operate on
its enclosing view’s coordinate space:

```
struct ContentView: View {
    @State private var location = CGPoint.zero

    var body: some View {
        VStack {
            Color.red.frame(width: 100, height: 100)
                .overlay(circle)
            Text("Location: \(Int(location.x)), \(Int(location.y))")
        }
        .coordinateSpace(name: "stack")
    }

    var circle: some View {
        Circle()
            .frame(width: 25, height: 25)
            .gesture(drag)
            .padding(5)
    }

    var drag: some Gesture {
        DragGesture(coordinateSpace: .named("stack"))
            .onChanged { info in location = info.location }
    }
}
```

Here, the [`VStack`](/documentation/SwiftUI/VStack) in the `ContentView` named “stack” is composed of a
red frame with a custom [`Circle`](/documentation/SwiftUI/Circle) view [`overlay(_:alignment:)`](/documentation/SwiftUI/View/overlay(_:alignment:))
at its center.

The `circle` view has an attached [`DragGesture`](/documentation/SwiftUI/DragGesture) that targets the
enclosing VStack’s coordinate space. As the gesture recognizer’s closure
registers events inside `circle` it stores them in the shared `location`
state variable and the [`VStack`](/documentation/SwiftUI/VStack) displays the coordinates in a [`Text`](/documentation/SwiftUI/Text)
view.

![A screenshot showing an example of finding a named view and tracking](images/com.apple.SwiftUI/SwiftUI-View-coordinateSpace@2x.png)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)