Skip to content

Instantly share code, notes, and snippets.

@DevAndArtist
Created October 3, 2019 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevAndArtist/110e7bae4a7a4066eb5ee42d08a0a08a to your computer and use it in GitHub Desktop.
Save DevAndArtist/110e7bae4a7a4066eb5ee42d08a0a08a to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State var flag = true
var body: some View {
let text = "\(flag ? "HStack" : "ZStack")"
return VStack {
Text(text)
Button("change the stack") {
self.flag.toggle()
}
Layout(flag: flag) {
NavigationView {
NavigationLink(destination: A()) {
Text("move to A")
}
}
}
}
}
}
struct A: View {
var body: some View {
NavigationLink(destination: B()) {
Text("move to B")
}
}
}
struct B: View {
var body: some View {
Text("B")
}
}
struct Layout<Content>: View where Content: View {
var flag: Bool
var content: Content
init(flag: Bool, content: () -> Content) {
self.flag = flag
self.content = content()
}
var hStack: some View {
HStack
{
content
}
.border(Color.red)
}
var zStack: some View {
ZStack
{
content
}
.border(Color.green)
}
@ViewBuilder
var body: some View {
if flag {
hStack
} else {
zStack
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment