I'm developing macOS Application using Swift UI. Everything is fine, but I have one question:
Is it possible to reuse View in the exact same it was left? (Scroll position, text inside views and other things)
So here is my code:
import SwiftUI
//Dummy View #1
struct HelloWorldView: View {
var body: some View {
Text("This is HelloWorld View --> \(arc4random())").bold()
}
}
//Dummy View #2
struct GoodbyeWorldView: View {
var body: some View {
Text("This is GoodbyeWorldView \(arc4random())").bold()
}
}
struct ContentView: View {
@State private var selection: String?
var body: some View {
NavigationView {
List (selection: $selection) {
Section(header: Text("Top")) {
NavigationLink(destination: HelloWorldView()) {
Text("First")
}
NavigationLink(destination: GoodbyeWorldView()) {
Text("Second")
}
}
}.listStyle(SidebarListStyle())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I have 2 dummy views, which can be accessed via NavigationLink(destination: *). Each view (HelloWorldView, GoodbyeWorldView) has a Text with arc4random() function.
Every time I'm switching between this views - value in Text element is changing. It tells me that these views are reinitialized and not reused in the previous state.
What I really want is to achieve TabView behavior, when you can switch between tabs without losing the view state.
TabView(selection: $selectionTabview){
HelloWorldView()
.tabItem {
Text("First")
}.tag(0)
GoodbyeWorldView()
.tabItem {
Text("Second")
}.tag(1)
}
Yes, with TabView I have the behavior I want, but TabView can't be set completely borderless (without tab buttons, borders, background).
So can I somehow replicate TabView behavior using some alternatives? ...Or maybe can I just remove borders, background, buttons from TabView?
Thanks.
P.S. I don't want to store arc4random() in variable to preserve its state. I just want to reuse views like TabView does.