如何在 SwiftUI 的特定屏幕中隐藏标签栏?

时间:2021-03-03 08:22:02

标签: ios swift swiftui tabbar swiftui-navigationview

如何在特定屏幕隐藏tabBar?我正在从登录导航到直接到 tabBar。有没有办法隐藏?在 UIKit 中,我们通过推送来隐藏,而我不知道如何在 SwiftUI 中通过呈现不起作用的视图来实现。

这是我的 TabBar

struct ReceiverTabBar: View {
    @State private var selection: Int = 0
  
    var body: some View {
        TabView(selection: $selection){
                .tabItem {
                    selection == 0 ? Image("")
                    Text("")
                }
                .tag(0)
            
            ReceiverProfileView()
                .tabItem {
                    selection == 1 ? Image("")
                    Text("")
                }
                .tag(1)
            ReceiverNotificationsView()
                .tabItem {
                    selection == 2 ? Image("")
                    Text("")
                }
                .tag(2)
            ReceiverMoreView()
                .tabItem {
                    selection == 3 ? Image("")
                    Text("")
                }
                .tag(3)
        }
        .accentColor(.black)
    }
}

我想在这个视图中隐藏 tabBar

struct MakingDonationView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @State var selected = 0
    
    var body: some View {
        
                ScrollView(showsIndicators: false) {
                            Image("")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .padding(.horizontal,30)
                                .padding(.top,40)
                                .frame(height: UIScreen.main.bounds.height/5)
                            
                                Text("")
                                    .font(.custom("Poppins-SemiBold", size: 16))
                                    .foregroundColor(Color("#252422"))
                                    .padding(.top,20)
                                
                                
                                Text("")
                                    .font(.custom("Poppins-SemiBold", size: 12))
                                    .foregroundColor(Color("#5E5E5E"))
                                
                                Text("")
                                    .font(.custom("Poppins-Medium", size: 12))
                                    .foregroundColor(Color("#A0A0A0"))
                            }
                            Spacer()
                            Divider()
                            
                            MakingDonation(selected: $selected)
                                
                        }
                        .padding(.all)
                        
                    }
                    .padding(.horizontal,20)
                    .edgesIgnoringSafeArea(.bottom)
                }
                        Button(action: {
                        }, label: {
                            
                            Spacer()
                            Text("Confirm Donation Amount")
                                .font(.custom("Poppins-SemiBold", size: 13))
                                .foregroundColor(.black)
                            Spacer()
                        })
                        .frame(height:44)
                        .background(Color("#FFA919"))
                        .padding(.horizontal,20)
                    }
                    .shadow(color: .gray, radius: 1, x: 0, y: 2)
                    .cornerRadius(4)
                    
                }
                .shadow(color: .gray, radius: 2, x: 0, y: 2)
                .edgesIgnoringSafeArea(.all)
                .frame(height:80)
                
                .navigationBarBackButtonHidden(true)
                .navigationBarTitle("Making Donation", displayMode: .inline)
            }
    }
    func goBack(){
        self.presentationMode.wrappedValue.dismiss()
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

// Container Screen view for handling all screen
struct ContainerView: View {
    @State var tabSelection: Screens = .screen1
    var body: some View {
        NavigationView{
            TabView(selection: $tabSelection){
                // Screen 1
                // Hide tab bar view only for Screen 1 
                NavigationLink(destination: PushedView()){
                    VStack{
                        Text("Screen 1")
                        Text("Tap to PushedView")
                    }
                }
                .tabItem { Text("Screen 1") }
                .tag(Screens.screen1)
                
                // Screen 2
                // same view using for screen 2 for directly shows on that
                PushedView()
                .tabItem { Text("Screen 2") }
                .tag(Screens.screen2)
            }
            .navigationBarTitle( self.tabSelection.title)
        }
    }
}

// New view for pushing
struct PushedView: View {
     var body: some View {
        Text("Hi! This is the new View")
            .navigationBarTitle("NewView")
     }
 }

// Tab screens
enum Screens{
    case screen1, screen2
    
    var title: String {
        switch self {
        case .screen1:
            return "Screen1"
        case .screen2:
            return "Screen2"
        }
    }
}