【发布时间】:2021-04-06 19:01:14
【问题描述】:
是否可以隐藏/删除 NavigationBar 但保持后退按钮可见?
我曾尝试隐藏整个导航栏并使用 NavigationLink 添加自定义后退按钮,但是当我按下后退时,我在第一个视图 (ProductList) 上获得了一个后退按钮,这是以前不存在的。
如果我必须创建一个自定义后退按钮,是否有不同于NavigationLink 的方法,就像我在Tab1View 中使用的那样,在导航返回时消除出现在ProductList 中的后退按钮?
这是第一个使用NavigationLink 到TabBar 的视图:
struct ProductList: View {
ScrollView{
VStack{
ForEach(matchedItems) { item in
NavigationLink(destination: TabView(product: item)){
Text("MoveToTabView")
}
}
}
}
}
}
然后我使用TabBar 隐藏NavigationBar:
struct TabView: View {
var product: ProductModel
var body: some View {
TabView {
// TAB 1
Tab1View(product: product).tabItem {
Text(product.detailTabNames[0])
}
.navigationBarTitle("")
.navigationBarHidden(true)
// TAB 2
Tab2View(product: product).tabItem {
Text(product.detailTabNames[1])
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
最后是Tabbar 中的最后一个View,我将在其中添加我自己的自定义后退按钮:
struct Tab1View: View {
var product: ProductModel
var body: some View {
ZStack(alignment: .topLeading) {
Text("Tab1View")
NavigationLink(destination: ProductList()){
Image(systemName: "chevron.backward")
}
}
}
}
【问题讨论】:
-
后退按钮是 NavigationBar 的组成部分。如果你隐藏栏,你需要自定义后退按钮。
-
@Asperi - 谢谢,我已经使用 NavigationLink 添加了一个自定义后退按钮,它位于代码的最后一点:NavigationLink(destination: ProductList()){ Image(systemName: "chevron .backward") }。通过这样做,它会导致我的原始视图 ProductList 有一个后退按钮,如果这有意义的话,原来的没有后退按钮?