Search code examples
swiftuiswiftui-navigationlinkswiftui-animationswiftui-navigationstackswiftui-navigationsplitview

New NavigationStack in SwiftUI transition, how to change from the default slide to custom or appear?


I have NavigationStack with its own navigation links, for iOS16 development.

How to change the default slide transition to appear or a custom transition?

Also do you apply the transition on the NavigationStack or on the NavigationLink?

This is my code:

import SwiftUI

struct TestView: View {
    var body: some View {
        NavigationStack() {
            NavigationLink ("Link", value: "Any Value")
                .navigationDestination(for: String.self) { textValue in
                    destinationView(textValue: textValue)
                }
        }
    }
}

struct destinationView: View {
    let textValue: String
    var body: some View {
            Text(textValue)
    }
}

How to change the default transition?


Solution

  • I don't think SwiftUI natively supports custom transition between Views when using NavigationStack / NavigationSplitView.

    There is a package called NavigationTransitions which supports NavigationStack on iOS 16.

    How to use:

    Add package to you project and import

    import NavigationTransitions
    

    Then:

    NavigationStack {
        // your content
    }
    .navigationTransition(.fade(.cross))
    

    You can also combine predefined transitions or make a custom one.