【发布时间】: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,隐藏 backbutton 和navbar 本身在 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 在从ExploreView 到MessagesView 的 NavigationLink 之后隐藏。
重申我的问题:
当您点击导航链接到 MessagesView 时,一切正常,但是当您向右滑动 little 以关闭 MessagesView (.onAppear { self.hideBar = true } 触发并隐藏 MessagesView 中的 NavBar,即使我还没有关闭它。
【问题讨论】: