SwiftUI SwiftUI Navigation 1 — Questions and Answers
Question 1: Which view provides a navigation stack with a back button in SwiftUI (iOS 16+)?
- NavigationView
- NavigationStack (Correct answer)
- NavigationSplitView
- TabView
Correct answer: NavigationStack
`NavigationStack` replaced `NavigationView` in iOS 16 as the preferred stack-based navigation container.
Question 2: What modifier triggers navigation to a destination view when a view is tapped in a NavigationStack?
- .navigationLink()
- .navigationDestination()
- NavigationLink wrapping the view
- Both B and C depending on iOS version (Correct answer)
Correct answer: Both B and C depending on iOS version
In iOS 16+ you can use `NavigationLink` or the data-driven `.navigationDestination(for:destination:)` modifier to navigate.
Question 3: What does `NavigationSplitView` provide in SwiftUI?
- A tabbed navigation interface
- A two- or three-column sidebar navigation layout (Correct answer)
- A modal sheet with columns
- A paged scroll navigation
Correct answer: A two- or three-column sidebar navigation layout
`NavigationSplitView` creates a sidebar/detail or sidebar/content/detail layout, adapting between iPad and iPhone automatically.
Question 4: How do you set the title shown in the navigation bar for a SwiftUI view?
- .title()
- .navigationTitle()
- .navigationBarTitle()
- Both B and C work (Correct answer)
Correct answer: Both B and C work
Both `.navigationTitle()` (iOS 14+) and the older `.navigationBarTitle()` set the navigation bar title.
Question 5: Which modifier adds a button to the navigation bar's trailing edge?
- .toolbar { ToolbarItem(placement: .navigationBarTrailing) { ... } }
- .navigationBarItems(trailing:)
- Both A and B work (Correct answer)
- .trailingButton()
Correct answer: Both A and B work
Both `.toolbar` with `ToolbarItem` (preferred in iOS 14+) and the older `.navigationBarItems(trailing:)` can add trailing bar buttons.
Question 6: What is the purpose of the `path` parameter in `NavigationStack(path:)`?
- It sets the navigation bar title path
- It provides a programmatic binding to the navigation stack's path for deep linking (Correct answer)
- It defines the root view path
- It specifies the animation path
Correct answer: It provides a programmatic binding to the navigation stack's path for deep linking
Binding a `NavigationPath` to the stack allows programmatic navigation, back-navigation, and deep link support.
Which view provides a navigation stack with a back button in SwiftUI (iOS 16+)?