One Logo Pool Ball
SwiftUICore · IOS API REFERENCE

ViewDimensions

struct · iOS 13.0+ · SDK 27.1

struct ViewDimensions

A view's size and alignment guides in its own coordinate space. This structure contains the size and alignment guides of a view. You receive an instance of this structure to use in a variety of layout calculations, like when you: * Define a default value for a custom alignment guide; see ``AlignmentID/defaultValue(in:)``. * Modify an alignment guide on a view; see ``View/alignmentGuide(_:computeValue:)``. * Ask for the dimensions of a subview of a custom view layout; see ``LayoutSubview/dimensions(in:)``. ### Custom alignment guides You receive an instance of this structure as the `context` parameter to the ``AlignmentID/defaultValue(in:)`` method that you implement to produce the default offset for an alignment guide, or as the first argument to the closure you provide to the ``View/alignmentGuide(_:computeValue:)`` view modifier to override the default calculation for an alignment guide. In both cases you can use the instance, if helpful, to calculate the offset for the guide. For example, you could compute a default offset for a custom ``VerticalAlignment`` as a fraction of the view's ``height``: private struct FirstThirdAlignment: AlignmentID { static func defaultValue(in context: ViewDimensions) -> CGFloat { context.height / 3 } } extension VerticalAlignment { static let firstThird = VerticalAlignment(FirstThirdAlignment.self) } As another example, you could use the view dimensions instance to look up the offset of an existing guide and modify it: struct ViewDimensionsOffset: View { var body: some View { VStack(alignment: .leading) { Text("Default") Text("Indented") .alignmentGuide(.leading) { context in context[.leading] - 10 } } } } The example above indents the second text view because the subtraction moves the second text view's leading guide in the negative x direction, which is to the left in the view's coordinate space. As a result, SwiftUI moves the second text view to the right, relative to the first text view, to keep their leading guides aligned: ![A screenshot of two strings. The first says Default and the second, which appears below the first, says Indented. The left side of the second string appears horizontally offset to the right from the left side of the first string by about the width of one character.](ViewDimensions-1-iOS) ### Layout direction The discussion above describes a left-to-right language environment, but you don't change your guide calculation to operate in a right-to-left environment. SwiftUI moves the view's origin from the left to the right side of the view and inverts the positive x direction. As a result, the existing calculation produces the same effect, but in the opposite direction. You can see this if you use the ``View/environment(_:_:)`` modifier to set the ``EnvironmentValues/layoutDirection`` property for the view that you defined above: ViewDimensionsOffset() .environment(\.layoutDirection, .rightToLeft) With no change in your guide, this produces the desired effect --- it indents the second text view's right side, relative to the first text view's right side. The leading edge is now on the right, and the direction of the offset is reversed: ![A screenshot of two strings. The first says Default and the second, which appears below the first, says Indented. The right side of the second string appears horizontally offset to the left from the right side of the first string by about the width of one character.](ViewDimensions-2-iOS)

Members (6)

get · iOS 13.0+

width

TypeScript
get width(): number
Swift declaration
var width: CGFloat { get }

The view's width.

get · iOS 13.0+

height

TypeScript
get height(): number
Swift declaration
var height: CGFloat { get }

The view's height.

subscript · iOS 13.0+

subscript

TypeScript
subscriptExplicit(guide: VerticalAlignment): (number) | null
Swift declaration
subscript(explicit guide: VerticalAlignment) -> CGFloat? { get }

Gets the explicit value of the given vertical alignment guide Find the vertical offset of a particular guide in the corresponding view by using that guide as an index to read from the context: .alignmentGuide(.top) { context in context[.top] - 10 } This subscript returns `nil` if no value exists for the guide. For information about using subscripts in Swift to access member elements of a collection, list, or, sequence, see [Subscripts](https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html) in _The Swift Programming Language_.

subscript · iOS 13.0+

subscript

TypeScript
subscriptExplicit74831(guide: HorizontalAlignment): (number) | null
Swift declaration
subscript(explicit guide: HorizontalAlignment) -> CGFloat? { get }

Gets the explicit value of the given horizontal alignment guide. Find the horizontal offset of a particular guide in the corresponding view by using that guide as an index to read from the context: .alignmentGuide(.leading) { context in context[.leading] - 10 } This subscript returns `nil` if no value exists for the guide. For information about using subscripts in Swift to access member elements of a collection, list, or, sequence, see [Subscripts](https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html) in _The Swift Programming Language_.

subscript · iOS 13.0+

subscript

TypeScript
subscript(guide: VerticalAlignment): number
Swift declaration
subscript(guide: VerticalAlignment) -> CGFloat { get }

Gets the value of the given vertical guide. Find the offset of a particular guide in the corresponding view by using that guide as an index to read from the context: .alignmentGuide(.top) { context in context[.top] - 10 } For information about using subscripts in Swift to access member elements of a collection, list, or, sequence, see [Subscripts](https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html) in _The Swift Programming Language_.

subscript · iOS 13.0+

subscript

TypeScript
subscript30079(guide: HorizontalAlignment): number
Swift declaration
subscript(guide: HorizontalAlignment) -> CGFloat { get }

Gets the value of the given horizontal guide. Find the offset of a particular guide in the corresponding view by using that guide as an index to read from the context: .alignmentGuide(.leading) { context in context[.leading] - 10 } For information about using subscripts in Swift to access member elements of a collection, list, or, sequence, see [Subscripts](https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html) in _The Swift Programming Language_.

Skipped declarations

No declarations were skipped in this snapshot.

All SwiftUICore types →