Home > AI > IOS > SwiftUI >

alignmentGuide(_:computeValue:)

Example :

struct ContentView: View {
    var body: some View {
        
        // Example 1
        VStack(alignment: .leading) {
            Text("Hello, world!")
                .alignmentGuide(.leading) { d in d[.leading] }
            Text("This is a longer line of text")
        }
        
        
        // Example 2
        VStack(alignment: .leading) {
            ForEach(0..<10) { position in
                Text("Number \(position)")
                    .alignmentGuide(.leading) { d in
                        print(d[.leading])
                        return d[.leading] + CGFloat(position)*5
                    }
            }
        }
        .background(Color.red)
        .frame(width: 400, height: 400)
        .background(Color.blue)
    }
}

Example 2 (Apple Official):

struct ContentView: View {
    var body: some View {
        
        VStack {
            Text("Today's Weather")
                .font(.title)
                .border(Color.gray)

            HStack {
                Text("&#x1f327;")
                    .alignmentGuide(VerticalAlignment.center) { _ in -20 }
                Text("Rain & Thunderstorms")
                    .border(Color.gray)
                Text("&#x26c8;")
                    .alignmentGuide(VerticalAlignment.center) { _ in 20 }
                    .border(Color.gray)
            }
        }
    }
}

Leave a Reply