简体   繁体   English

我在 SwiftUI 中创建了一个自定义选项卡栏,但是当我使用 select 选项卡时,它会完全重新加载视图。 我该如何防止这种情况

[英]I created a custom tab bar in SwiftUI, but when I select a tab, it completely reloads the view. How do I prevent this

  1. I created a custom tab bar我创建了一个自定义标签栏
  2. I am using a switch statement to switch between my views我正在使用 switch 语句在我的视图之间切换
  3. The problem is that the view is being reloaded everytime I switch between tabs问题是每次我在选项卡之间切换时都会重新加载视图
  4. How do I "save" the state of the view when I switch between views?在视图之间切换时,如何“保存”视图的 state?

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.因此,例如,如果我在第一个选项卡的视图中并且我在滚动视图上向下滚动一点,然后我 go 到第二个选项卡,然后返回到第一个选项卡,它会完全重新加载视图并且它不会表明我已经向下滚动页面。

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()
            }
        }
    }
    
}

} }

There are a few ways you could go about doing this.有几种方法可以让你做到这一点。

Method 1: Changing Z-Index方法 1:更改 Z-Index

Rather than showing either one view or another, you could keep both views being drawn at all times, but display the current, active view on top of the inactive views.您可以始终保持两个视图都被绘制,而不是显示一个视图另一个视图,但在非活动视图之上显示当前的活动视图。 You can do this by using the .zIndex() modifier.您可以使用.zIndex()修饰符来做到这一点。

Method 2: Using @SceneStorage (or @AppStorage)方法二:使用@SceneStorage(或@AppStorage)

The @SceneStorage and @AppStorage property wrappers both enable data persistence. @SceneStorage@AppStorage属性包装器都支持数据持久性。 For either one, you should keep track of the value you want to persist (such as the scroll height or something), and store it in SceneStorage/AppStorage.对于任何一个,您都应该跟踪您想要保留的值(例如滚动高度或其他内容),并将其存储在 SceneStorage/AppStorage 中。

@AppStorage is usually used for storing user preferences or constants, and would be the worse out of the two options, but is still possible. @AppStorage通常用于存储用户首选项或常量,在这两个选项中会更糟,但仍然可能。 What you should , ideally, use is @SceneStorage .理想情况下,您应该使用的是@SceneStorage Essentially, @SceneStorage lets you store small amounts of data in the "scene", such as how far the user has scrolled.本质上, @SceneStorage允许您在“场景”中存储少量数据,例如用户滚动了多远。 However, this data is destroyed when the scene is destroyed, such as when the user closes the app.但是,当场景被销毁时,例如当用户关闭应用程序时,这些数据也会被销毁。 You can find out more here你可以在这里找到更多

Also, if you're wondering how to store how far a user has scrolled in a ScrollView , you should use a ScrollViewReader :)此外,如果您想知道如何存储用户在ScrollView中滚动的距离,您应该使用ScrollViewReader :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM