NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How to adjust the position of a view using its offset

Paul Hudson    @twostraws   

Updated for Xcode 12.0

All views have a natural position inside your hierarchy, but the offset() modifier lets you move them relative to that natural position. This is particularly useful inside ZStack, where it lets you control how views should overlap.

Important: Using offset() will cause a view to be moved relative to its natural position, but won’t affect the position of other views. This means you can make one view overlap another when normally it would have been positioned next to it, which may not be what you want.

For example, in this VStack we can use offset() to move the second item down by 15 points so that it begins to overlap the third item:

VStack {
        .offset(y: 15)

You will commonly find that using padding() together with offset() gives you the result you’re looking for, as that moves one view around while also adjusting the views next to it to match.

For example, this will move the second item down by 15 points, but add 15 points of padding to its bottom edge so that it doesn’t overlap the text view below:

VStack {
        .offset(y: 15)
        .padding(.bottom, 15)

When used in conjunction with a ZStack, offsets let us position one view inside another, which is particularly useful when you control the alignment of the ZStack. For example, if we have a ZStack showing a photo along with the name of the photographer, we might use bottomTrailing alignment to make the image take up all the available space while having the credit line sit in the bottom-right corner, then use offset(x: -5, y: -5) to pull the credit line back by five points:

ZStack(alignment: .bottomTrailing) {
    Text("Photo: \(item.photoCredit)")
        .offset(x: -5, y: -5)
Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.