Created
August 7, 2022 21:08
-
-
Save gromwel/93999a3bb44f3f769d3b6381a04afcd8 to your computer and use it in GitHub Desktop.
Custom Vertical Horizontal alignment
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
struct ContentView: View { | |
var body: some View { | |
VStack(spacing: 40) { | |
HStack(alignment: .midLeftAndRight) { | |
VStack { | |
Text("Left 1") | |
Text("Left 2") | |
.alignmentGuide(.midLeftAndRight) { | |
$0[.bottom] / 2 | |
} | |
Text("Left 3") | |
Text("Left 4") | |
Text("Left 5") | |
} | |
VStack { | |
Text("Right 1") | |
.alignmentGuide(.midLeftAndRight) { | |
$0[.bottom] / 2 | |
} | |
Text("Right 2") | |
} | |
} | |
VStack(alignment: .midTopAndBottom) { | |
HStack { | |
Text("Top 1") | |
Text("Top 2") | |
.alignmentGuide(.midTopAndBottom) { | |
$0[.trailing] / 2 | |
} | |
Text("Top 3") | |
Text("Top 4") | |
Text("Top 5") | |
} | |
HStack { | |
Text("Bottom 1") | |
.alignmentGuide(.midTopAndBottom) { | |
$0[.trailing] / 2 | |
} | |
Text("Bottom 2") | |
} | |
} | |
} | |
} | |
} | |
extension VerticalAlignment { | |
private enum MidLeftAndRight: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { | |
context[.bottom] / 2 | |
} | |
} | |
static let midLeftAndRight = VerticalAlignment(MidLeftAndRight.self) | |
} | |
extension HorizontalAlignment { | |
private enum MidTopAndBottom: AlignmentID { | |
static func defaultValue(in context: ViewDimensions) -> CGFloat { | |
context[.trailing] / 2 | |
} | |
} | |
static let midTopAndBottom = HorizontalAlignment(MidTopAndBottom.self) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment