UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Project 12: What is the difference between .alignmentGuide() modifier and .offset() modifier?

Forums > macOS

@mtm  

According to the book, "This result is different from using the offset() modifier: if you offset a text its original dimensions don’t actually change, even though the resulting view is rendered in a different location. If we had offset the first text view rather than changing its alignment guide, the VStack wouldn’t expand to contain it."

I'll happily agree that the first VStack is easier to understand, however both of the VStack below appear to render exactly the same output?

struct ContentView: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("Number 0")
                .alignmentGuide(.leading) { d in d[.trailing] }
                .border(.red)
            Text("Number 1")
                .border(.red)
        }
        .border(.blue)
        Spacer()
        VStack(alignment: .leading) {
            Text("Number 0")
                    .border(.red)
            Text("Number 1")
                .alignmentGuide(.leading) {_ in -60 }
                .border(.red)
        }
        .border(.blue)
    }
}

3      

.alignmentGuide() modifier: This modifier aligns a view with a specific alignment guide within the container view. Alignment guides are invisible lines that SwiftUI uses to align views within a container. For example, you can use the .alignmentGuide(.leading) modifier to align a view with the leading edge of the container. You can also create your own custom alignment guides to align views in more specific ways. The .alignmentGuide() modifier takes a closure that returns a CGFloat, which specifies the offset of the view from the alignment guide.

.offset() modifier: This modifier offsets a view by a specific amount in the horizontal and/or vertical directions. You can use the .offset() modifier to move a view from its original position within the container. The .offset() modifier takes a CGSize, which specifies the horizontal and vertical offset of the view.

3      

.alignmentGuide() is used for aligning views within a container, influencing how they align with each other. .offset() is used for adjusting the position of a view relative to its natural position, without affecting the alignment of other views. Both modifiers offer flexibility in layout customization and can be used together or independently depending on the specific requirements of your SwiftUI views.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.