SwiftUI show/hide NavigationBar 的标题问题
SwiftUI show/hide title issues with NavigationBar
我有以下代码构造,这给我带来了很多麻烦:
//Main View
struct ContentView: View {
var body: some View {
NavigationView{
ZStack(alignment: .center){
CarouselBuilder()
ProfileInvoke().navigationBarTitle("").navigationBarHidden(true)
}
}
}
}
//Carousel filled with Cards from a DB
...code irrelevant for my problem
//Profile Invoke -> Invokes a slide out menu called Menu that has NavigationLinks in it
struct Menu: View {
var body: some View {
ZStack{
VStack(alignment: .center){
MenuButton(buttonText: "Settings", buttonCallView: AnyView(SettingsView() ))
MenuButton(buttonText: "My Favourites", buttonCallView: AnyView(MyFavouritesView()))
MenuButton(buttonText: "Sign Out", buttonCallView: AnyView(SignOutView()))
}.frame(width: UIScreen.main.bounds.width/1.2,alignment: .top)
}
}
}
//MenuButtons are basic NavigationLinks linking to certain Views given as argument when calling them
我现在将主视图中的 ZStack
包装在 NavigationView 中,我需要这样做才能使 NavigationLinks
正常工作。我还必须在此 "top" 级别上执行此操作,因为我需要新视图,该视图将由滑出菜单中的链接调用以占据整个屏幕,而不仅仅是显示滑出视图的宽度。
我现在的问题是,我当然不希望导航栏在主视图中占据 space。为此,我将它的隐藏属性设置为 true。这会贯穿整个应用程序,并且还会禁用菜单中按钮链接到的子视图中的导航视图。这让我无法回头。
我的问题是:
1)有没有更优雅的方式来做这一切?
2) 如何 re-invoke 子视图中的导航栏? (将它们的隐藏导航栏属性设置回 false 无效。
以下是在根视图中隐藏导航栏并在子视图中显示的可能方法。唯一需要的修改是在根视图中。
测试 Xcode 11.4 / iOS 13.4
这里只是一个根,子视图是常规的,不需要针对这种情况的特殊代码。查看内联重要说明。
struct RootNavigationView: View {
@State private var hideBar = true // << track hide state, and default
var body: some View {
NavigationView {
VStack {
Text("I'm ROOT")
Divider()
NavigationLink("Goto Child", destination: NextChildView(index: 1))
.simultaneousGesture(TapGesture().onEnded {
self.hideBar = false // << show, here to be smooth !!
})
}
.navigationBarHidden(hideBar)
// .navigationBarTitle("Back to Root") // << optional
.onAppear {
self.hideBar = true // << hide on back
}
}
}
}
我有以下代码构造,这给我带来了很多麻烦:
//Main View
struct ContentView: View {
var body: some View {
NavigationView{
ZStack(alignment: .center){
CarouselBuilder()
ProfileInvoke().navigationBarTitle("").navigationBarHidden(true)
}
}
}
}
//Carousel filled with Cards from a DB
...code irrelevant for my problem
//Profile Invoke -> Invokes a slide out menu called Menu that has NavigationLinks in it
struct Menu: View {
var body: some View {
ZStack{
VStack(alignment: .center){
MenuButton(buttonText: "Settings", buttonCallView: AnyView(SettingsView() ))
MenuButton(buttonText: "My Favourites", buttonCallView: AnyView(MyFavouritesView()))
MenuButton(buttonText: "Sign Out", buttonCallView: AnyView(SignOutView()))
}.frame(width: UIScreen.main.bounds.width/1.2,alignment: .top)
}
}
}
//MenuButtons are basic NavigationLinks linking to certain Views given as argument when calling them
我现在将主视图中的 ZStack
包装在 NavigationView 中,我需要这样做才能使 NavigationLinks
正常工作。我还必须在此 "top" 级别上执行此操作,因为我需要新视图,该视图将由滑出菜单中的链接调用以占据整个屏幕,而不仅仅是显示滑出视图的宽度。
我现在的问题是,我当然不希望导航栏在主视图中占据 space。为此,我将它的隐藏属性设置为 true。这会贯穿整个应用程序,并且还会禁用菜单中按钮链接到的子视图中的导航视图。这让我无法回头。
我的问题是: 1)有没有更优雅的方式来做这一切? 2) 如何 re-invoke 子视图中的导航栏? (将它们的隐藏导航栏属性设置回 false 无效。
以下是在根视图中隐藏导航栏并在子视图中显示的可能方法。唯一需要的修改是在根视图中。
测试 Xcode 11.4 / iOS 13.4
这里只是一个根,子视图是常规的,不需要针对这种情况的特殊代码。查看内联重要说明。
struct RootNavigationView: View {
@State private var hideBar = true // << track hide state, and default
var body: some View {
NavigationView {
VStack {
Text("I'm ROOT")
Divider()
NavigationLink("Goto Child", destination: NextChildView(index: 1))
.simultaneousGesture(TapGesture().onEnded {
self.hideBar = false // << show, here to be smooth !!
})
}
.navigationBarHidden(hideBar)
// .navigationBarTitle("Back to Root") // << optional
.onAppear {
self.hideBar = true // << hide on back
}
}
}
}