Created
June 14, 2022 15:36
-
-
Save HarshilShah/bde8b20991e6452797e17bac46283bbe to your computer and use it in GitHub Desktop.
A SwiftUI view to render an HStack with centered, leading, and trailing contents
This file contains 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 | |
struct SymmetricHStack<Content: View, Leading: View, Trailing: View>: View { | |
@ViewBuilder var content: () -> Content | |
@ViewBuilder var leading: () -> Leading | |
@ViewBuilder var trailing: () -> Trailing | |
var body: some View { | |
HStack { | |
ZStack(alignment: .leading) { | |
trailing().hidden() | |
leading() | |
} | |
Spacer() | |
content() | |
Spacer() | |
ZStack(alignment: .trailing) { | |
leading().hidden() | |
trailing() | |
} | |
} | |
} | |
} | |
extension SymmetricHStack where Leading == EmptyView { | |
init( | |
@ViewBuilder content: @escaping () -> Content, | |
@ViewBuilder trailing: @escaping () -> Trailing | |
) { | |
self.content = content | |
self.leading = EmptyView.init | |
self.trailing = trailing | |
} | |
} | |
extension SymmetricHStack where Trailing == EmptyView { | |
init( | |
@ViewBuilder content: @escaping () -> Content, | |
@ViewBuilder leading: @escaping () -> Leading | |
) { | |
self.content = content | |
self.leading = leading | |
self.trailing = EmptyView.init | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment