Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Last active May 1, 2022 17:30
Show Gist options
  • Save andresr-dev/af9add149ec7d9fbee1b44fc74a29516 to your computer and use it in GitHub Desktop.
Save andresr-dev/af9add149ec7d9fbee1b44fc74a29516 to your computer and use it in GitHub Desktop.
This is how you can make custom alignments and how to use it. In this case we do a custom alignment for two separate VStacks
import SwiftUI
extension VerticalAlignment {
enum MidAccountAndName: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
context[.top]
}
}
static let midAccountAndName = VerticalAlignment(MidAccountAndName.self)
}
//MARK: HOW TO USE IT
struct CustomAlignmentGuide: View {
var body: some View {
HStack(alignment: .midAccountAndName) {
VStack {
Text("@andresr_develop")
.alignmentGuide(.midAccountAndName) { d in
d[VerticalAlignment.center]
}
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 64, height: 64)
}
.background(.green)
VStack {
Text("Full name:")
Text("Andrés Raigoza".uppercased())
.alignmentGuide(.midAccountAndName) { d in
d[VerticalAlignment.center]
}
.font(.title)
}
.background(.blue)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment