【问题标题】:How to fix a bug of half closed View SwiftUi如何修复 View SwiftUi 半关闭的 bug
【发布时间】:2020-05-28 05:13:57
【问题描述】:

我有一个带有.navigationBarHidden(hideBar)RootView(@State var hideBar = true)和一个带有 .simultaneousGesture(TapGesture().onEnded { self.hideBar = false })ChildView,这使得RootView 在转换后自动将hideBar 设置为false。因为我不希望我的RootView 有导航栏,所以我设置了 .onAppear {self.hideBar = true }。

问题是当我做出向右滑动手势 (from ChildView back to RootView) 时,即使我没有完全滑动 (RootView,它仍然会自动设置 hideBar back to true,隐藏 backbuttonnavbar 本身在 ChildView 上。

有没有办法解决这个问题?非常感谢任何帮助!

这是我的代码:

struct ExploreView: View {

     @State private var hideBar = true

    var body: some View {
        
        VStack{
            HStack{

                       
                    NavigationLink(destination:MessagesView()){

                         Image("messages")
                        
                      }.simultaneousGesture(TapGesture().onEnded {
                         self.hideBar = false     
                      })
                .foregroundColor(Color("blackAndWhite"))

            }.padding(EdgeInsets(top: 5, leading: 20, bottom: 0, trailing: 20))                
                 

        }
         .navigationBarTitle(Text(""), displayMode: .inline)
        .navigationBarHidden(hideBar)
         .onAppear {
          self.hideBar = true  // hides on back
         }
 
            
    }
}

这里是 TabBarView:

struct TabBarView: View {

    
    @State var selection: Int = 0
    @State var index: Int = 0

    var body: some View {
          ZStack{
            GeometryReader { geometry in
                
        NavigationView{
                TabView(selection: self.$selection){
                        
                          ExploreView()
                        
//                          .navigationBarHidden(true)
                        
                          .tabItem{
                            VStack{
                                Image("clostNav").renderingMode(.template)
                                                            
                                                           
                                Text("Explore")
                            
                            }.foregroundColor(Color("blackAndWhite"))
                            
                             
                              
                              
                    }.tag(0)
                    
                    SearchView()
                        .navigationBarTitle(Text(""), displayMode: .inline)
                       .navigationBarHidden(true)
                        .tabItem{
                             
                              Image("search").renderingMode(.template)
                              Text("Search")
                       }.tag(1)
                          
                       PostView()
                       .navigationBarTitle(Text(""), displayMode: .inline)
                        .navigationBarHidden(true)
                       .tabItem{
                           HStack{
                              
                                  Image("post").renderingMode(.template)
                                .resizable().frame(width: 35, height: 35)
                                
                            
                              }
                              
                          }.tag(2)

                          OrdersView()
                            .navigationBarTitle(Text(""), displayMode: .inline)
                            .navigationBarHidden(true)
                          .tabItem{
                              
                              Image("orders").renderingMode(.template)
                              Text("Orders")

                             
                                

                          }.tag(3)
                   
                        ProfileView()
//                    .navigationBarTitle(Text(""), displayMode: .inline)
//                    .navigationBarHidden(true)
                          
                          
                            
                          .tabItem{
                              
                             Image("profile").renderingMode(.template)
                              
                              Text("Profile")
   
                          }.tag(4)
                    
                  }
                
               }.accentColor(Color("redMain"))
            
                .opacity(self.selection == 2 ? 0.001 : 1)
                PostView()
                    .offset(x: geometry.size.width / 2 - 30, y: geometry.size.height - 80)
                    .onTapGesture {
                        self.selection = 0
                }
                .opacity(self.selection == 2 ? 1 : 0)
                
            }
        }
    }
        
}

解释:

我把我的Tabbar{} 放在NavigationView 里面是为了让bottomTabbar 在从ExploreViewMessagesView 的 NavigationLink 之后隐藏。

重申我的问题:

当您点击导航链接到 MessagesView 时,一切正常,但是当您向右滑动 little 以关闭 MessagesView (.onAppear { self.hideBar = true } 触发并隐藏 MessagesView 中的 NavBar,即使我还没有关闭它。

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    像这样使用 onDisappear 并删除同时的手势,然后它对我有用:

    struct ExploreView: View {
    
        @State private var hideBar = true
    
        var body: some View {
    
            VStack{
                HStack{
                    NavigationLink(destination:EmptyView()){
                        Image(systemName: "mic")
                    }
                }.padding(EdgeInsets(top: 5, leading: 20, bottom: 0, trailing: 20))
            }
            .navigationBarTitle(Text(""), displayMode: .inline)
            .navigationBarHidden(hideBar)
            .onAppear {
                self.hideBar = true  // hides on back
            }
            .onDisappear {
                self.hideBar = false
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答!我更新了代码和解释。尝试了您的解决方案,不幸的是,它并没有解决我的问题,因为我在 TabBarView 中有 NavView。
    • 非常感谢!有效!最后一个问题,你的观点不是在哪里导航到反弹吗? (因为我们 show 然后 hide 然后 show 一次)你知道怎么解决吗?如果有办法就完美了..
    • 我会制作一个没有此类问题的自定义 NavigationBar。我可以在一个单独的问题中为您提供代码,或者您在 Google 中为它提供代码。
    • 谢谢。如果您在新问题中与我分享您的代码,我将不胜感激。我现在就创建它。
    猜你喜欢
    • 2020-03-22
    • 2021-10-28
    • 2022-06-27
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多