I have a TabView
that shows 2 tabs:
struct TestInitial: View {
var body: some View {
TabView {
Text("Content 1")
.tabItem { Text("Tab 1") }
Text("Content 2")
.tabItem { Text("Tab 2") }
}
}
}
I wanted to add something similar to a paywall to access content, which I did like so:
struct Test: View {
@State private var isAuthorised = false
var body: some View {
TabView {
Group {
if isAuthorised {
Text("Content 1")
} else {
BlockView()
}
}
.tabItem { Text("Tab 1") }
Group {
if isAuthorised {
Text("Content 2")
} else {
BlockView()
}
}
.tabItem { Text("Tab 2") }
}
}
}
Using Group
and a Conditional
like this makes it unnecessarily hard to read in my opinion. I wonder, is there a simple way to share the same view between each tab? In addition to readability, in contexts where BlockView
had some state, it would be beneficial for SwiftUI to use the same memory for each tab.
Is there any way to tell SwiftUI that each tab is using the same view?