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

2

6

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?

Halfcocked answered 4/3, 2023 at 13:6 Comment(0)
D
6

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.

Deportation answered 4/3, 2023 at 19:36 Comment(1)
Thanks, this is exactly what I was looking to do. It's a shame that the new navigationStack and swiftui do not have native options, also people are getting quite tired of constant changes that brake every previous code or constantly deprecating previews protocols, etcHalfcocked
C
0

iOS 18 introduces a new API called NavigationTransition, which allows you to customize your transitions (including the oh-so-popular zoom transition). Of course, that's not going to help you if you're still targeting iOS 17 or earlier. But it's something to keep in mind so that you can structure your existing code in a way that will be easier to refactor if/when you want to adopt the new API.

Czechoslovakia answered 21/8, 2024 at 9:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.