Last active
May 1, 2022 17:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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