[英]iOS SwiftUI - NavigationView NavigationLink: Close view with custom Button
I'm new to iOS development and think it's awesome, BUT I absolutely HATE everything about NavigationView/NavigationLink, even in Android it's very hard to find such idiocy!我是 iOS 开发的新手,认为它很棒,但我绝对讨厌 NavigationView/NavigationLink 的一切,即使在 Android 中也很难找到这样的白痴!
In ContentView I have:在 ContentView 我有:
NavigationLink(destination: Login().navigationBarBackButtonHidden(true), label: {
Image(systemName: "person.circle.fill")
.resizable()
.aspectRatio(contentMode: .fit).frame(width: 32)
.foregroundColor(Color(UIColor(named: "IconColor")!))
})
And Login():和登录():
struct Login: View {
@AppStorage("userid") var userid: Int = 0
var body: some View{
VStack{
HStack{
Spacer()
Button(action: {
// Close the view!!???
}) {
Image(systemName: "xmark")
.resizable()
.frame(width: 18, height: 18)
.foregroundColor(Color(UIColor(named: "IconColor")!))
}
.padding(.top, 12.0)
.padding(.trailing, 16.0)
}
Spacer()
}
}
}
How can I just close the View clicking on the button?我怎样才能关闭单击按钮的视图?
You would use presentationMode.wrappedValue.dismiss()
.您将使用
presentationMode.wrappedValue.dismiss()
。 Take a look at this for more information.看看这个以获取更多信息。 Here is how you would use it:
以下是您将如何使用它:
struct Login: View {
@AppStorage("userid") var userid: Int = 0
//Doesn't require anything to be passed in.
@Environment(\.presentationMode) var presentationMode
var body: some View{
VStack{
HStack{
Spacer()
Button(action: {
//Part where view is dismissed
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "xmark")
.resizable()
.frame(width: 18, height: 18)
.foregroundColor(Color(UIColor(named: "IconColor")!))
}
.padding(.top, 12.0)
.padding(.trailing, 16.0)
}
Spacer()
}
}
}
You would still call this the same way, with a NavigationLink
like in the original question above.您仍然会以同样的方式调用它,使用上面原始问题中的
NavigationLink
。
Take a look at this answer for more info on dismissing.查看此答案以获取有关解雇的更多信息。 Also, note that this will disable the default swipe-back behavior, if you want to re-enable it, take a look here .
另外,请注意,这将禁用默认的向后滑动行为,如果您想重新启用它,请查看此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.