- I created a custom tab bar
- I am using a switch statement to switch between my views
- The problem is that the view is being reloaded everytime I switch between tabs
- How do I "save" the state of the view when I switch between views?
So for example, if I am in the view of the first tab and I scroll down a bit on my scrollview, and I go to the 2nd tab, and then back to the first tab, it completely reloads the view and it doesn't show that I have scrolled down the page.
The Code:
struct Home: View {
@State var selectedIndex = 0
@State var presented = false
let icons = ["house", "hands.sparkles", "plus", "message", "person"]
var body: some View {
VStack{
Spacer().fullScreenCover(isPresented: $presented, content: {
Text("Create Post Here")
Button(action: {
presented.toggle()
}, label: {
Text("Close")
})
})
switch selectedIndex{
case 0:
HomePage()
case 1:
NavigationView(){
Discover()
}
case 2:
LogoutForNow()
case 3:
LogoutForNow()
case 4:
NavigationView(){
Profile2()
}
default:
HomePage()
}
HStack{
ForEach(0..<5, id: \.self){number in
Spacer()
Button(action: {
if number == 2{
presented.toggle()
}else{
self.selectedIndex = number
}
}, label: {
if number == 2{
Image(systemName: icons[number])
.font(.system(size: 25, weight: .regular, design: .default))
.foregroundColor(.white)
.frame(width: 50, height: 50)
.background(Color.blue)
.cornerRadius(25)
}else{
Image(systemName: icons[number])
.font(.system(size: 25, weight: .regular, design: .default))
.foregroundColor(selectedIndex == number ? .black : Color(UIColor.lightGray))
}
})
Spacer()
}
}
}
}
}