SwiftUI: How to switch to a new navigation stack with NavigationViews
This is how we solved this question: We had a main/root/launch view, from which the user could tap a button to start a business workflow of some kind. This would open a sheet, which would display a modal pop-up view. (The width and height of sheets can be customised, in order to take up most/all of the screen.)
The sheet will have a NavigationView. This would allow the user to step through a series of views as part of their workflow. The "presented" flag gets passed as a binding from the main view to each navigated view.
When the user reaches the last view and taps a Submit/Done/Finish button to end that particular workflow, the "presented" binding can be set to false, which closes the modal pop-up, and returns the user back to the main view.
SwiftUI - Switching views with NavigationView
There are many ways of doing this. Here is one.
import SwiftUI
enum MainViewTypes: String{
case comments
case posts
}
struct ParentView: View {
@SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
var body: some View {
switch mainViewType{
case MainViewTypes.comments.rawValue:
//Comments View
VStack{
Text("comments")
Button("go to posts", action: {
mainViewType = MainViewTypes.posts.rawValue
})
}
case MainViewTypes.posts.rawValue:
//Posts view
VStack{
Text("posts")
PostRowView()
}
default:
Button("unknown", action: {
mainViewType = MainViewTypes.posts.rawValue
})
}
}
}
struct PostRowView: View {
@SceneStorage("mainViewType") var mainViewType: String = MainViewTypes.posts.rawValue
var body: some View {
Button("go to comments", action: {
mainViewType = MainViewTypes.comments.rawValue
})
}
}
struct ParentView_Previews: PreviewProvider {
static var previews: some View {
ParentView()
}
}
SwiftUI How do I navigate on Child View to a new navigation page
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView(.vertical, showsIndicators: false){
VStack {
Text("Content View text")
Image("contentviewimage")
ForEach((1...10), id: \.self) { user in
UserView()
}
}
}
}
}
}
struct UserView: View {
var body: some View {
VStack {
Image("bob")
Text("Hello my name is bob")
NavigationLink(
destination: BobView()){
Text("Click me to navigate").font(.system(size: 20))
}
}
}
}
struct BobView: View{
var body: some View{
VStack{Text("bob view")}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Unwind NavigationView to root when switching tabs in SwiftUI
You'll need to keep track of the tab selection in the parent view and then pass that into the child views so that they can watch for changes. Upon seeing a change in the selection, the child view can then reset a @State
variable that change the isActive
property of the NavigationLink
.
class NavigationManager : ObservableObject {
@Published var activeTab = 0
}
struct MyTabView: View {
@StateObject private var navigationManager = NavigationManager()
var body: some View {
TabView(selection: $navigationManager.activeTab) {
TabOne().tabItem { Image(systemName: "1.square") }.tag(0)
TabTwo().tabItem { Image(systemName: "2.square") }.tag(1)
}.environmentObject(navigationManager)
}
}
struct TabOne: View {
var body: some View {
Text("1")
}
}
struct TabTwo: View {
@EnvironmentObject private var navigationManager : NavigationManager
@State private var linkActive = false
var body: some View {
NavigationView {
NavigationLink("Go to sub view", isActive: $linkActive) {
TabTwoSub()
}
}.onChange(of: navigationManager.activeTab) { newValue in
linkActive = false
}
}
}
struct TabTwoSub: View {
var body: some View {
Text("Tapping \(Image(systemName: "1.square")) doesnt unwind this view back to the root of the NavigationView")
.multilineTextAlignment(.center)
}
}
Note: this will result in a "Unbalanced calls to begin/end appearance transitions" message in the console -- in my experience, this is not an error and not something we have to worry about
Related Topics
Why Are Objects in the Same Sknode Layer Not Interacting with Each Other
Any or a Trouble with Sequence of Functions
How to Correctly Do Up an Adjustable Split View in Swiftui
How to Access Content View's Elements Later in Swiftui
How to Move Application's Window Between Virtual Desktops in Os X
Swift Enumerate Functions. How Does It Work Behind
How to Trigger Xcode's 'Update to Latest Package Versions' from Command Line
Convert Optional String to Int in Swift
iOS Corebluetooth: Startadvertising() Error Advertising Static Data
Can My Class Override Protocol Property Type in Swift
Delegate Property with Different Type in Swift
Nonfailable Enum Initializer with Default Value
How to Hide the Back Button from the Status Bar on the Apple Watch
Swift: Generics and Type Constraints, Strange Behavior