SwiftUI NavigationView/Stack if available iOS 15/16
Asked Answered
P

1

2

There's something I don't really understand about the transition from NavigationView to NavigationStack for SwiftUI 4 and iOS 16.

I have 2 applications running on the App Store (targeting iOS 15 and above) and of course I'm using NavigationView. This weekend, I was looking for a solution to have them ready for iOS 16 by replacing:

var body: some View {
   NavigationView {
      ScrollView { /* code here */ }
   }
}

To something like this:

var body: some View {
   if #available(iOS 16, *) {
      NavigationStack {
   } else {
      NavigationView {
   }
      ScrollView { /* code here */ }
   }
}

But finally, before doing so, I tried them with iOS 16 Beta and they're perfectly fine. I don't understand, I thought it would be total a mess but finally no. Do you have an explanation? Maybe, deprecated isn't what it should mean?

Pich answered 5/9, 2022 at 8:53 Comment(1)
If you're looking for the solution btw: #73701350Pich
N
3

NavigationView is deprecated, not obsoleted. When a component becomes deprecated, it still can be used until Apple decides to obsolete it which will cause a crash when used. Although it's functional, you shouldn't use it.

Nauseate answered 5/9, 2022 at 10:35 Comment(3)
Thanks a lot @Timmy. I really appreciate the clarification. I think I'll push an update with NavigationStack. I'm just wondering if my second piece of code is ok? Or if there's a better way to make it works also for iOS 15 with NavigationView.Pich
I suggest you create a custom container, otherwise it is absolutely okay!Nauseate
Alright, that's all I needed to know. Thanks again @Timmy. ✌️Pich

© 2022 - 2024 — McMap. All rights reserved.