我在 SwiftUI 中隐藏导航栏时遇到困难。尽管使用了 .navigationBarHidden(true),导航栏仍然可见。 这是我的代码:
struct LogInPage: View {
// some state variables
var body: some View {
NavigationView {
ZStack {
// some code including a HStack
VStack(spacing: 35) {
// some code
Toggle("", isOn: $showPassword)
.offset(x: -185)
Button(action: {
print("Log in button was pressed")
}) {
Text("Logga in")
.foregroundColor(.white)
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(red: 0.6, green: 0.24, blue: 0.73)))
}
.offset(y: 20)
HStack(spacing: 15) {
// some code
}
.offset(y: 30)
Button(action: {
print("Register button was pressed")
isNavigating = true
}) {
Text("Skapa konto")
.foregroundColor(.white)
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color(red: 0.6, green: 0.24, blue: 0.73)))
}
.offset(y: 60)
NavigationLink(
destination: RegisterPage(),
isActive: $isNavigating,
label: {
EmptyView()
}
)
}
}
.navigationBarHidden(true) // trying to hide my navigation bar here
}
}
}
我尝试了很多不同的方法来解决这个问题:
.navigationBarHidden(true)
,没有任何改变 .navigationBarHidden(true)
,没有任何改变
.navigationBarBackButtonHidden(true)`.navigationBarHidden(true) .navigationBarBackButtonHidden(true) .navigationBarTitle("", displayMode: .inline)
,这实际上帮助我隐藏了导航栏文本,但箭头仍然存在。.navigationBarHidden(true)
,但这仍然没有改变任何东西。我认为我的问题与子视图以及我如何构建代码有关。我不是专业人士,我确信我的代码结构不是那么专业。如有任何帮助,我们将不胜感激。
为什么要将内容包裹在
NavigationView
内并且还想隐藏导航视图?只需删除外部NavigationView
。应该可以。
var body: some View {
//NavigationView { <- remove
ZStack {
...
}
.navigationBarHidden(true)
//}
}