英文:
How hide navigation bar always back from any view directly using NavigationView?
问题
我正在使用xcode-14.2和最低目标版本14。我有三个视图 ContentView
、Welcome
和 FundTransfer
。以下是我的情况。
- ContentView - 加载第一个视图,
navigationBarHidden
正常工作。当单击Welcome
页按钮时,它转到Welcome
页。 - Welcome 视图 - 当单击
Fund Transfer
按钮时,它转到FundTransfer
视图。 FundTransfer
- 当单击Log out
按钮时,它转到ContentView
。
流程如下:ContentView -> FundTransfer -> ContentView
问题:当从 FundTransfer
视图返回到 ContentView
时,导航栏会显示出来。这意味着从 FundTransfer
视图返回到 ContentView
时会显示导航栏,而在一开始时是隐藏的。
如何始终从任何视图直接返回到 ContentView
时隐藏导航栏?
以下是我的代码:
ContentView:
struct ContentView: View {
@State private var showWelcome = false
@State var isNavigationBarHidden: Bool = true
var body: some View {
NavigationView {
VStack {
ScrollView {
VStack(alignment: .customCenter,spacing: 0){
VStack {
SubmitButton(action: {
self.showWelcome = true
}) {
Text("Welcome page")
}
}
NavigationLink(destination: Welcome(), isActive: $showWelcome) { EmptyView() }
}
}
}
.navigationBarTitle("") //this must be empty
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}
Welcome View:
struct Welcome: View {
@State private var showFundTransfer = false
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
ScrollView {
VStack(alignment: .customCenter,spacing: 0){
VStack {
SubmitButton(action: {
showFundTransfer = true
}) {
Text("Fund Transfer")
}
}
NavigationLink(destination: FundTransfer(), isActive: $showFundTransfer) { EmptyView() }
}
}
.navigationBarHidden(true)
}
}
}
FundTransfer View:
struct FundTransfer: View {
@State var isNavigationBarHidden: Bool = true
@State private var logon = false
var body: some View {
VStack {
ScrollView {
VStack(alignment: .customCenter,spacing: 0){
SubmitButton(action: {
self.logon = true
}) {
Text("Log out")
}
}
}
NavigationLink(destination: ApplicationSwitcher(), isActive: $logon) { EmptyView() }.opacity(0)
}
.navigationBarHidden(true)
}
}
请帮助我。
英文:
I am using xcode-14.2 & minimum target version 14. I have three views ContentView
, Welcome
& `FundTransfer. Here is my case.
- ContentView - Load first view & navigationBarHidden is working. When
Welcome
page button click it goes toWelcome
page - Welcome view - When
Fund Transfer
button is clicked, it goes toFundTransfer
view FundTransfer
- whenLog out
button is clicked, it goes toContentView
It goeslike: ContentView-> FundTransfer-> ContentView
Problem: When it goes from FundTransfer
view to ContentView
it shows navigationBar. That means when back from FundTransfer
view to ContentView
shows navigationBar which was hidden at the first.
How do I hide navigation bar always back from any view directly to ContentView
?
Here is my code:
ContentView:
struct ContentView: View {
@State private var showWelcome = false
@State var isNavigationBarHidden: Bool = true
var body: some View {
NavigationView {
VStack {
ScrollView {
VStack(alignment: .customCenter,spacing: 0){
VStack {
SubmitButton(action: {
self.showWelcome = true
}) {
Text("Welcome page")
}
}
NavigationLink(destination: Welcome(), isActive: $showWelcome) { EmptyView() }
}
}
}
.navigationBarTitle("") //this must be empty
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}
Welcome View:
struct Welcome: View {
@State private var showFundTransfer = false
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
ScrollView {
VStack(alignment: .customCenter,spacing: 0){
VStack {
SubmitButton(action: {
showFundTransfer = true
}) {
Text("Fund Transfer")
}
}
NavigationLink(destination: FundTransfer(), isActive: $showFundTransfer) { EmptyView() }
}
}
.navigationBarHidden(true)
}
}
}
FundTransfer View:
struct FundTransfer: View {
@State var isNavigationBarHidden: Bool = true
@State private var logon = false
var body: some View {
VStack {
ScrollView {
VStack(alignment: .customCenter,spacing: 0){
SubmitButton(action: {
self.logon = true
}) {
Text("Log out")
}
}
}
NavigationLink(destination: ApplicationSwitcher(), isActive: $logon) { EmptyView() }.opacity(0)
}
.navigationBarHidden(true)
}
}
Please help me..
答案1
得分: 1
在NavigationLink中也添加.navigationBarHidden(true)例如:
NavigationLink(destination: ApplicationSwitcher()
.navigationBarHidden(true), isActive: $logon) { EmptyView() }.opacity(0)
英文:
Add .navigationBarHidden(true) in NavigationLink also for eg:
NavigationLink(destination: ApplicationSwitcher()
.navigationBarHidden(true), isActive: $logon) { EmptyView() }.opacity(0)
答案2
得分: 1
在ContentView的NavigationView的闭包之后,按照下面的方式添加"navigationBarHidden(true)"而不是VStack:
NavigationView {
...
}.navigationBarTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论