Instance Method

offset(x:y:)

Offset this view by the specified horizontal and vertical distances.

Declaration

func offset(x: CGFloat = 0, y: CGFloat = 0) -> some View

Parameters

x

The horizontal distance to offset this view.

y

The vertical distance to offset this view.

Return Value

A view that offsets this view by x and y.

Discussion

Use offset(x:y:) to to shift the displayed contents by the amount specified in the x and y parameters.

The original dimensions of the view are not changed by offsetting the contents; in the example below the gray border drawn by this view surrounds the original position of the text:

Text("Offset by passing horizontal & vertical distance)")
    .border(Color.green)
    .offset(x: 20, y: 50)
    .border(Color.gray)
A screenshot showing a view that offset from its original position using and x and y offset.

See Also

Positioning a View

func position(CGPoint) -> View

Positions the center of this view at the specified point in its parent‘s coordinate space.

func position(x: CGFloat, y: CGFloat) -> View

Positions the center of this view at the specified coordinates in its parent’s coordinate space.

func offset(CGSize) -> View

Offset the view by the horizontal and vertical amount specified in the offset parameter.

func edgesIgnoringSafeArea(Edge.Set) -> View

Change the view’s proposed area to extend outside the screen’s safe areas.

func coordinateSpace<T>(name: T) -> View

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.