is there a way in SwiftUI to change the standard behaviour of NavigationView/NavigationLink, which is to slide from one view to the next? I'd like to have a custom animation/transition like fade in/out.
I'm trying out things for a while now and solved half of it but struggling with the second half. This code:
struct ContentView: View {
@State var appeared: Double = 0.0
var body: some View {
NavigationView {
VStack {
Text("\(appeared)")
NavigationLink(destination: ViewB()) {Text("go to View B")}
}
.opacity(appeared)
.animation(Animation.easeInOut(duration: 3.0), value: appeared)
.onAppear {self.appeared = 1.0}
.onDisappear {self.appeared = 0.0}
}
}
}
Works correct when the view appears. It fades in by changing opacity to 1.0 within 3 seconds. All good. But when clicking the NavigationLink
, to go to View B, it slides away this view and View B (which has the same logic) fades in slowly. I guess this is because onDisappear
is actually triggered when the view is gone, not when it is about to move away.
Are there any chances to tell SwiftUI to not swipe or slide but do a custom animation/transition?
I'm using Xcode 11.2.1
NavigationView
animation, but it is possible to create custom navigation. You might find helpful the approach I provided in this post. There were requested slide animation there, but actually it can be repacked with any one. – Ibnsaud