我一直在尝试查找如何在 SwiftUI 中添加自定义导航栏后退按钮,但我得到了这种奇怪的行为,即默认行为仍然出现在显示自定义行为之前。有谁知道添加它的正确方法吗?
这是我尝试过的。
var body: some View {
NavigationView {
ZStack {
Color.background.edgesIgnoringSafeArea(.all)
NavigationLink(destination: UserDetailsView()) {
Text("Continue")
.foregroundColor(.background)
.font(.title)
.fontWeight(.semibold)
}
.frame(width: 250, height: 60, alignment: .center)
.background(Color.white)
.cornerRadius(40)
.padding(.top, 50)
}
.navigationBarTitle("", displayMode: .automatic)
.navigationBarHidden(true)
}
}
这里,当我使用下面的代码隐藏后退按钮时,它根本没有隐藏。
.navigationBarBackButtonHidden(true)
在详细信息屏幕
var body: some View {
NavigationView {
VStack {
ZStack {
Rectangle()
.foregroundColor(.clear)
.background(gradient)
.edgesIgnoringSafeArea(.all)
Text("Hello")
}
}
.navigationBarItems(leading: BackButton(presentationMode: presentationMode))
}
}
自定义后退按钮如下所示
struct BackButton: View {
var presentationMode : Binding<PresentationMode>
var body: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image(Icon.leftArrow)
.aspectRatio(contentMode: .fit)
.foregroundColor(.black)
}
}
}
}
最佳答案
感谢您的提示。它对我有用......但是在添加
之后.navigationBarItems(leading: ...
滑动返回功能不再起作用。 我猜这是 SwiftUI 中的一个错误?
有人找到解决方法吗?
关于ios - 如何在 SwiftUI 中添加自定义导航栏后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691957/