Skip to content

Instantly share code, notes, and snippets.

@zaksoup
Created July 1, 2020 04:38
Show Gist options
  • Save zaksoup/efc09700748d2f21a21b12b5b5e4c4d7 to your computer and use it in GitHub Desktop.
Save zaksoup/efc09700748d2f21a21b12b5b5e4c4d7 to your computer and use it in GitHub Desktop.
A playground where I try to understand ViewBuilders
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
@State var presses = 0
var body: some View {
VStack {
Text("swiftui!")
.font(.headline)
.padding(.top)
Divider()
TabView {
PushMeView(presses: $presses).tabItem {
Text("PushMe!")
Image(systemName: "1.square.fill")
}
ShowMeView(presses: presses).tabItem {
Text("ShowMe!")
Image(systemName: "2.square.fill")
}
}
}
}
}
struct WrapperView<Content: View>: View {
let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
VStack {
content
.padding()
Divider()
/* something here like
For each view in the content put a divider beneath it
*/
}
}
}
struct ShowMeView: View {
var presses: Int
var body: some View {
WrapperView {
Text("Pushed \(presses) times!")
Text("Another text 2")
}
}
}
struct PushMeView: View {
@Binding var presses: Int
var body: some View {
VStack {
Text("Pushed \(presses) times!")
Button(action: {
self.presses += 1
}, label: {
Text("Push me!")
})
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment