如何修复半关闭 View SwiftUi 的错误 [英] How to fix a bug of half closed View SwiftUi

查看:26
本文介绍了如何修复半关闭 View SwiftUi 的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 .navigationBarHidden(hideBar) (@State var hideBar = true) 的 RootView 和带有 ChildView .concurrentGesture(TapGesture().onEnded { self.hideBar = false }) 是什么让 RootView 在转换后自动将 hideBar 设置为 false.由于我不希望我的 RootView 有导航栏,所以我设置了 .onAppear {self.hideBar = true }.

问题是当我做出向右滑动手势(从 ChildView 回到 RootView)时,即使我没有完全滑动(<一半)到 RootView,它仍然会自动将 hideBar 设置回 true,在 ChildView 上隐藏 backbuttonnavbar 本身.>

有什么办法可以解决这个问题吗?非常感谢您的帮助!

这是我的代码:

struct ExploreView:查看{@State 私有 var hideBar = truevar主体:一些视图{虚拟堆栈{堆栈{导航链接(目的地:消息视图()){图像(消息")}.simultaneousGesture(TapGesture().onEnded {self.hideBar = false}).foregroundColor(颜色(blackAndWhite"))}.padding(EdgeInsets(顶部:5,领先:20,底部:0,尾随:20))}.navigationBarTitle(Text(""), displayMode: .inline).navigationBarHidden(hideBar).onAppear {self.hideBar = true//隐藏在背面}}}

这是 TabBarView:

struct TabBarView:查看{@State var 选择:Int = 0@State var 索引:Int = 0var主体:一些视图{ZStack{GeometryReader { 几何输入导航视图{TabView(selection: self.$selection){探索视图()//.navigationBarHidden(true).tabItem{虚拟堆栈{图像(clostNav").renderingMode(.template)文本(探索")}.foregroundColor(颜色(blackAndWhite"))}.tag(0)搜索视图().navigationBarTitle(Text(""), displayMode: .inline).navigationBarHidden(真).tabItem{图像(搜索").renderingMode(.template)文本(搜索")}.tag(1)后视图().navigationBarTitle(Text(""), displayMode: .inline).navigationBarHidden(真).tabItem{堆栈{图像(post").renderingMode(.template).resizable().frame(宽度:35,高度:35)}}.tag(2)订单视图().navigationBarTitle(Text(""), displayMode: .inline).navigationBarHidden(真).tabItem{图像(订单").renderingMode(.template)文本(订单")}.tag(3)个人资料视图()//.navigationBarTitle(Text(""), displayMode: .inline)//.navigationBarHidden(true).tabItem{Image(profile").renderingMode(.template)文本(个人资料")}.tag(4)}}.accentColor(颜色(redMain")).opacity(self.selection == 2 ? 0.001 : 1)后视图().offset(x: geometry.size.width/2 - 30, y: geometry.size.height - 80).onTapGesture {自我选择 = 0}.opacity(self.selection == 2 ? 1 : 0)}}}}

说明:

我将我的 Tabbar{} 放在 NavigationView 中,以使 bottomTabbar 隐藏在 ExploreView 的 NavigationLink 之后到 MessagesView.

重申我的问题:

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

解决方案

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

struct ExploreView:查看{@State 私有 var hideBar = truevar主体:一些视图{虚拟堆栈{堆栈{NavigationLink(目的地:EmptyView()){图像(系统名称:麦克风")}}.padding(EdgeInsets(顶部:5,领先:20,底部:0,尾随:20))}.navigationBarTitle(Text(""), displayMode: .inline).navigationBarHidden(hideBar).onAppear {self.hideBar = true//隐藏在背面}.onDisappear {self.hideBar = false}}}

I have a RootView with .navigationBarHidden(hideBar) ( @State var hideBar = true) and a ChildView with .simultaneousGesture(TapGesture().onEnded { self.hideBar = false }) what makes RootView automatically set hideBar to false after transition. Since I don't want my RootView to have a navbar, I set .onAppear {self.hideBar = true }.

The problem is when I make a swiping right gesture (from ChildView back to RootView), even tho I don't fully swipe (< half) to RootView, it still automatically sets hideBar back to true, hiding a backbutton and navbar itself on the ChildView.

Is there any ideas of how to cope with this problem ? Would appreciate any help!

Here is my code:

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

Here is 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)
                
            }
        }
    }
        
}

Explanation:

I put my Tabbar{} inside of NavigationView in order to make bottomTabbar hide after NavigationLink from ExploreView to MessagesView.

Restating my problem:

When you click on NavLink to MessagesView, everything works perfectly, but when you swipe right a little to close MessagesView (.onAppear { self.hideBar = true } triggers and hides NavBar in MessagesView even tho i have not closed it yet).

解决方案

Use onDisappear like this and delete the simultaneousGesture, then it worked for me:

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

这篇关于如何修复半关闭 View SwiftUi 的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆