GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Proxyman.

SPONSORED Debug 10x faster with Proxyman. Your ultimate tool to capture HTTPs requests/ responses, natively built for iPhone and macOS. You’d be surprised how much you can learn about any system by watching what it does over the network.

Try Now!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.