Unfortunately it's still not possible to customise the NavigationView
transition animations in SwiftUI (xCode 11.3.1).
Looking for a way to do that I ended up creating this open source project called swiftui-navigation-stack
(https://github.com/biobeats/swiftui-navigation-stack) that contains the NavigationStackView
, a view that mimics the navigation behaviours of the standard NavigationView
adding some useful features. For example you can turn off the transition animations or you can customise them with the transition you prefer.
Months ago I asked here above how to turn off transition animations. To do that you can use the NavigationStackView
this way:
struct ContentView : View {
var body: some View {
NavigationStackView(transitionType: .none) {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
PushView(destination: View2()) {
Text("PUSH")
}
}
}
}
}
struct View2: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
PopView {
Text("POP")
}
}
}
}
If you specify .none
as transitionType
you can turn off the animations. PushView
and PopView
are two views that allow you push and pop views (similar to the SwiftUI NavigationLink
).
The result is:
If you, instead, want to customise the transition animation (as I asked in the question here above with the cross-fade example) you can do like this:
struct ContentView : View {
let navigationTransition = AnyTransition.opacity.animation(.easeOut(duration: 2))
var body: some View {
NavigationStackView(transitionType: .custom(navigationTransition)) {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
PushView(destination: View2()) {
Text("PUSH")
}
}
}
}
}
struct View2: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
PopView {
Text("POP")
}
}
}
}
For a cross-fase transition I specified:
let navigationTransition = AnyTransition.opacity.animation(.easeOut(duration: 2))
a cross fade transition that lasts 2 seconds. Then I passed this transition to the NavigationStackView
:
NavigationStackView(transitionType: .custom(navigationTransition))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…