SwiftUI-在NavigationView中显示项目的动态列表时,动态隐藏和显示选项卡会导致问题

问题描述

这是主要的ContentView:

struct ContentView: View {

    @EnvironmentObject var displayDetails: DisplayDetails

    @State var selected = 0

    var body: some View {
    
        VStack {
        
        ContactList()
        
        Spacer()
        
        ZStack(alignment: .top) {
            
            if (!displayDetails.showFullScreen) {
                
                BottomBar(selected: self.$selected)
                    .padding()
                    .padding(.horizontal,22)
                    .background(CurvedShape())
                
                Button(action: {
                    
                }) {
                    
                    Image("Wishlist")
                        .renderingMode(.original)
                        .padding(18)
                    
                }
                .background(Color.yellow)
                .clipShape(Circle())
                .offset(y: -32)
                .shadow(radius: 5)
                
            }
            
        }
        
        }
        .background(Color("Color").edgesIgnoringSafeArea(.top))

    }

}

struct CurvedShape : View {

    var body : some View {
    
    Path { path in
        
        path.move(to: CGPoint(x: 0,y: 0))
        path.addLine(to: CGPoint(x: UIScreen.main.bounds.width,y: 55))
        
        path.addArc(center: CGPoint(x: UIScreen.main.bounds.width / 2,y: 55),radius: 30,startAngle: .zero,endAngle: .init(degrees: 180),clockwise: true)
        
        path.addLine(to: CGPoint(x: 0,y: 55))
        
    }
    .fill(Color.white)
    .rotationEffect(.init(degrees: 180))

    }

}

struct BottomBar : View {

    @Binding var selected : Int

    var body : some View{
    
    HStack{
        
        Button(action: {
            
            self.selected = 0
            
        }) {
            
            Image("home")
            
        }
        .foregroundColor(self.selected == 0 ? .black : .gray)
        
        Spacer(minLength: 12)
        
        Button(action: {
            
            self.selected = 1
            
        }) {
            
            Image("search")
            
        }
        .foregroundColor(self.selected == 1 ? .black : .gray)
        
        Spacer().frame(width: 120)
        
        Button(action: {
            
            self.selected = 2
            
        }) {
            
            Image("person")
            
        }
        .foregroundColor(self.selected == 2 ? .black : .gray)
        .offset(x: -10)

        Spacer(minLength: 12)
        
        Button(action: {
            
            self.selected = 3
            
        }) {
            
            Image("menu")
            
        }
        .foregroundColor(self.selected == 3 ? .black : .gray)
        
    }
    
    }

}

这是详细视图:

struct DetailView: View {

    let contact: Contact

    @EnvironmentObject var displayDetails: DisplayDetails

    var body: some View {
    
    VStack {
    
        Image(contact.imageName)
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 150,height: 150)
            .clipped()
            .cornerRadius(150)
            .shadow(radius: 3)
        
        Text(contact.name)
            .font(.title)
            .fontWeight(.medium)
        
        Form {
        
            Section {
            
                HStack {
                
                    Text("Phone")
                    
                    Spacer()
                    
                    Text(contact.phone)
                        .foregroundColor(.gray)
                        .font(.callout)
                
                }
                
                HStack {
                
                    Text("Email")
                    
                    Spacer()
                    
                    Text(contact.email)
                        .foregroundColor(.gray)
                        .font(.callout)
                
                }
                
                HStack {
                
                    Text("Address")
                    
                    Spacer()
                    
                    Text(contact.address)
                        .foregroundColor(.gray)
                        .font(.callout)
                        .frame(width: 180)
                
                }
            
            }
            
            Section {
            
                Button(action: {
                
                    print("Send a message")
                
                }) {
                    
                    Text("Send a message")
                
                }
                
                Button(action: {
                
                    print("Call")
                
                }) {
                
                    Text("Call")
                
                }
            
            }
        
        }
    
    }
    .onDisappear() {
        
        self.displayDetails.showFullScreen.toggle()
    
    }
    .onAppear(){
    
        self.displayDetails.showFullScreen.toggle()
    
    }
    .environment(\.colorScheme,.dark)
        
    }

}

这是NavigationView列表(ContactList):

struct ContactList: View {

    @ObservedObject private var viewModel = ContactViewModel()

    var body: some View {

    NavigationView {
    
        List(viewModel.contacts) { contact in
            
            NavigationLink(destination: DetailView(contact: contact)) {
            
                ContactRow(contact: contact)
            
            }
        
        }
        .navigationBarTitle("Contacts")
        .onAppear(){
            
            self.viewModel.fetchdata()
            
        }
    
    }
    .environment(\.colorScheme,.dark)

    }

}



struct ContactRow: View {

    let contact: Contact

    var body: some View {
    
    HStack {

        Image(contact.imageName)
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 60,height: 60)
            .clipped()
            .cornerRadius(50)

        VStack(alignment: .leading) {

            Text(contact.name)
                .font(.system(size: 21,weight: .medium,design: .default))

            Text(contact.phone)

        }

    }

    }

}

最后是根据需要隐藏或可见的标签栏切换的ObservableObject:

class DisplayDetails: ObservableObject {

    // Used to move to full screen when detail view is displayed in application
    @Published var showFullScreen: Bool = false

}

您需要做的就是将Firebase Firestore(或其他来源)设置为NavigationView(列表)的动态数据

以下是针对Firebase的简单教程:https://firebase.google.com/docs/ios/setup

(您将需要在GCP中创建Firestore实例,并d / l json文件并添加到项目中)

我无法使导航正常工作,因为NavigationView将无法正常工作!?我不确定这是Firebase问题还是SwiftUI问题!最有可能的SwiftUI。

如您所见,在导航到项目列表的导航上,列表本身会回滚到第一个项目并消失?

**问题**,在您没有亲自创建我为您查看行为的示例项目的情况下:

当用户单击列表中的项目时,它将导航到详细视图并根据需要隐藏标签栏。 **当过渡开始时,导航过渡到详细视图时,列表视图会汇总列表中的项目(很奇怪)?我希望只是一张详细视图的幻灯片(当项目列表是静态的时,例如在硬编码数组中,就会发生这种情况)。当用户单击后退按钮返回列表时;将显示该列表(就像从列表中的第一项展开一样),然后该列表再次回滚到第一项中,并且屏幕最终变为空白!?

有人遇到过此问题,您能够解决它吗??

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...