SwiftUI 仅在查看 ContentView 时隐藏导航栏
SwiftUI hiding a navigation bar only when looking at ContentView
我有一个内容文件,我隐藏了导航栏,因为它占用 space 并将元素向下推。 ContentView 中的一个按钮重定向(使用导航 link)到另一个视图。在另一个视图中,导航栏仍然隐藏....为了简单起见,我将从 ContentView 中删除一些代码:
//this is the view that looks "fine" (i.e. the navigation bar takes up no space)
struct ContentView: View {
@State private var isPresentedSettings = false
var body: some View {
NavigationView {
ZStack {
VStack {
SettingsButton(isPresentedSettings: $isPresentedSettings)
}
}.navigationBarTitle("").navigationBarHidden(true)
}
}
}
//this is the button that pulls up the settings page view
struct SettingsButton: View {
@Binding var isPresentedSettings: Bool
var body: some View {
NavigationLink (destination: SettingsPageView(isPresentedSettings:
self.$isPresentedSettings)) {
Button(action: { self.isPresentedSettings.toggle() }, label: { Text("Button") })
}
}
}
//This is the view that should have a navigationbar but it doesn't
struct SettingsPageView: View {
@Binding var isPresentedSettings: Bool
var body: some View {
NavigationView {
VStack {
Text("This is a view")
}.navigationBarTitle("Settings", displayMode: .inline)
}
}
}
另外...可能有拼写错误,因为我只是从另一台计算机上复制代码。抱歉,提前谢谢你!
首先,您不需要这个 isPresentedSettings
变量来呈现 NavigationLink
。
NavigationLink(destination: SettingsPageView()) {
Text("Button")
}
并且您的视图层次结构中应该只有一个 NavigationView
。
您的最终代码如下所示:
struct ContentView: View {
@State private var navBarHidden = true
var body: some View {
NavigationView {
ZStack {
VStack {
SettingsButton(navBarHidden: $navBarHidden)
}
}
.navigationBarHidden(navBarHidden)
}
}
}
struct SettingsButton: View {
@Binding var navBarHidden: Bool
var body: some View {
NavigationLink(destination: SettingsPageView(navBarHidden: $navBarHidden)) {
Text("Show View")
}
}
}
struct SettingsPageView: View {
@Binding var navBarHidden: Bool
var body: some View {
VStack {
Text("This is a view")
}
.navigationBarTitle("Settings", displayMode: .inline)
.onAppear {
self.navBarHidden = false
}
.onDisappear {
self.navBarHidden = true
}
}
}
我有一个内容文件,我隐藏了导航栏,因为它占用 space 并将元素向下推。 ContentView 中的一个按钮重定向(使用导航 link)到另一个视图。在另一个视图中,导航栏仍然隐藏....为了简单起见,我将从 ContentView 中删除一些代码:
//this is the view that looks "fine" (i.e. the navigation bar takes up no space)
struct ContentView: View {
@State private var isPresentedSettings = false
var body: some View {
NavigationView {
ZStack {
VStack {
SettingsButton(isPresentedSettings: $isPresentedSettings)
}
}.navigationBarTitle("").navigationBarHidden(true)
}
}
}
//this is the button that pulls up the settings page view
struct SettingsButton: View {
@Binding var isPresentedSettings: Bool
var body: some View {
NavigationLink (destination: SettingsPageView(isPresentedSettings:
self.$isPresentedSettings)) {
Button(action: { self.isPresentedSettings.toggle() }, label: { Text("Button") })
}
}
}
//This is the view that should have a navigationbar but it doesn't
struct SettingsPageView: View {
@Binding var isPresentedSettings: Bool
var body: some View {
NavigationView {
VStack {
Text("This is a view")
}.navigationBarTitle("Settings", displayMode: .inline)
}
}
}
另外...可能有拼写错误,因为我只是从另一台计算机上复制代码。抱歉,提前谢谢你!
首先,您不需要这个 isPresentedSettings
变量来呈现 NavigationLink
。
NavigationLink(destination: SettingsPageView()) {
Text("Button")
}
并且您的视图层次结构中应该只有一个 NavigationView
。
您的最终代码如下所示:
struct ContentView: View {
@State private var navBarHidden = true
var body: some View {
NavigationView {
ZStack {
VStack {
SettingsButton(navBarHidden: $navBarHidden)
}
}
.navigationBarHidden(navBarHidden)
}
}
}
struct SettingsButton: View {
@Binding var navBarHidden: Bool
var body: some View {
NavigationLink(destination: SettingsPageView(navBarHidden: $navBarHidden)) {
Text("Show View")
}
}
}
struct SettingsPageView: View {
@Binding var navBarHidden: Bool
var body: some View {
VStack {
Text("This is a view")
}
.navigationBarTitle("Settings", displayMode: .inline)
.onAppear {
self.navBarHidden = false
}
.onDisappear {
self.navBarHidden = true
}
}
}