SwiftUI:单击 NavigationLink 时后退按钮消失 [英] SwiftUI: Back button disappears when clicked on NavigationLink
问题描述
我正在尝试在屏幕顶部添加一个 NavigationLink
,但是一旦我点击它,它就会提示我结果并且返回按钮消失.
I'm trying to add a NavigationLink
at the top of the screen, but once I click it, it prompts me to the result and the Back button disappears.
SwiftUI 代码:
SwiftUI Code:
NavigationView {
VStack {
NavigationLink (destination: Text("COOL")) {
Text("COOL")
}
Spacer()
}
.navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
//.edgesIgnoringSafeArea([.top, .bottom])
}
点击NavigationLink
后后退按钮消失:https://gyazo.com/9d39936c849f5970a40a0
The back button disappears after clicking on the NavigationLink
: https://gyazo.com/9d39936c849f570a05687e41096ddeca
推荐答案
恕我直言,当您同时使用 .navigationBarHidden(true)
和 .navigationBarTitle(Text("Sometext))
.如果你删除最后一个,后退按钮照常工作.不过我试图在你的代码片段中返回后退按钮.返回第一个视图时它仍然有故障,但后退按钮不会消失. 我希望它会有所帮助,你会从这里走得更远:
There is some glitch IMHO, when you use both .navigationBarHidden(true)
and .navigationBarTitle(Text("Some text))
. If you remove the last one, back button works as usual. Nevertheless I tried to return back button in your code snippet. It still has glitch while returning to first view, but back button don't disappear. I hope it will help and you will go further from here:
struct NotHiddenBackButton: View {
@State var hiddingNavBar = true
@State var goToSecondView = false
var body: some View {
NavigationView {
NavigationLink(destination: ViewWithBackButton(hiddingNavBar: $hiddingNavBar), isActive: $goToSecondView) {
VStack {
Text("COOL")
.onTapGesture {
self.hiddingNavBar = false
self.goToSecondView = true
}
Spacer()
}
}
.navigationBarHidden(hiddingNavBar)
.navigationBarTitle(Text("Home"))
}
}
}
struct ViewWithBackButton: View {
@Binding var hiddingNavBar: Bool
var body: some View {
Text("Second view")
.navigationBarTitle("Second view")
.onDisappear() {
self.hiddingNavBar = true
}
}
}
这篇关于SwiftUI:单击 NavigationLink 时后退按钮消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!