2

The usual flow is the stack. Push views and pop views. My requirements is a bit complex and I did not find a way to do it in IOS 16.

Let say we have a

  • Cover page
  • Contents page
  • Details page

Now, the requirements;

  1. Cover page can push the Details page ; this is easy.
  2. Cover page can push the Contents page ; this is also easy.
  3. Contents page can push the Details page ; this is also easy.

Now;

  1. Whatever way the Details page is pushed (1 or 3), it must return to Contents page

Is this a possible requirement? If so, any guidance is welcome.

PS: sorry no code since there is no solution that I've figured!

kelalaka
  • 5,064
  • 5
  • 27
  • 44
  • Sounds like you are looking after how to navigate back to the root page. Check the accepted answer here in StackOverflow – N Subedi Mar 11 '23 at 19:15
  • @NSubedi not exactly. The contents page may not be in the stack if called from the cover page ( root). However, the details page needs to back to contents page. I know that Q/A and doesn't fit to my problem (comment corrected!) – kelalaka Mar 11 '23 at 19:33
  • So if the path has contents page go to it if not put it on the path? – lorem ipsum Mar 11 '23 at 19:40
  • @loremipsum I've looked that but could not find a way. This is at most depth 3 stack and the stack operation only allow push and pop, there is no insert, or I'm missing something? – kelalaka Mar 11 '23 at 19:45
  • Why would you need to insert? A navigation path that you can examine in an array you handle it like any array. – lorem ipsum Mar 11 '23 at 21:40
  • @loremipsum to be honest, I've never seen an example around. Do you have any? I'll dig more about it to solve my issue. – kelalaka Mar 12 '23 at 17:16
  • @loremipsum According to [hackingwithswift](https://www.hackingwithswift.com/quick-start/swiftui/how-to-use-programmatic-navigation-in-swiftui), NavigationPath is a stack. Also, I don't see any function to insert on docs https://developer.apple.com/documentation/swiftui/navigationpath . If you are sugesting to use an List to path of NavigationStack, as in the first example of the first link, then it can solve my issue, however, it is not NavigationPath. – kelalaka Mar 12 '23 at 18:35
  • 1
    Look at the “presentedNumbers” variable in that link, it is an array of Int – lorem ipsum Mar 12 '23 at 18:43
  • @loremipsum I've finally made it. Thanks a lot. I found that the transition to inserted page has no animation and failed to produce one, yet! – kelalaka Mar 12 '23 at 20:57

1 Answers1

0

NavigationPath is a stack that doesn't allow insertion. Instead one can use array as used in hackingwithswift. Each page is represented with a number except the root page.

There is a nice feature of the path; one doesn't need to dismiss the page with environmental presentation mode (see). Once the last number is removed the page dismissed.

The insertion is easy, however, the animation doesn't exist when returning to the inserted page. This is a bug, I think.

Yes, the new navigation opens the path to complex navigations.

import SwiftUI

struct NavPathTestView: View {
    
    @State var presentedNumbers = [Int]()

        var body: some View {
            NavigationStack(path: $presentedNumbers) {
                NavigationLink(value: 1) {
                    HStack {
                        Text("Contents")
                    }.padding()
                        .foregroundColor(.white)
                        .background(Color.red)
                        .cornerRadius(40)
                }
                NavigationLink(value: 2) {
                    HStack {
                        Text("Reader Page")
                    }.padding()
                        .foregroundColor(.white)
                        .background(Color.red)
                        .cornerRadius(40)
                }

                .navigationDestination(for: Int.self) { i in
                    if i == 1 {
                        ContentsPageView(presentedNumbers: $presentedNumbers)
                    } else {
                        ReaderPageView(presentedNumbers: $presentedNumbers)
                    } 
                }
                .navigationTitle("Cover Page")
            }
        }
}

struct ReaderPageView: View {
    
    @Binding var presentedNumbers : [Int]
    
    var body: some View {
        
        
        VStack {
            Text("Reader Page")
            Spacer()
            Button (action: {
                
                if (presentedNumbers.count == 2) && ( presentedNumbers.last == 2 ) {
                    presentedNumbers.removeLast()
                } else {
                    withAnimation() {
                        presentedNumbers.insert(1, at: 0)
                        presentedNumbers.removeLast()
                    }
                }
            })
            {
                HStack {
                    Text("Back To Contents")
                }.padding()
                    .foregroundColor(.white)
                    .background(Color.red)
                    .cornerRadius(40)
            }
        }
    }


}


struct ContentsPageView: View {
      
    @Binding var presentedNumbers : [Int]
    
    var body: some View {
        
        VStack() {
            
            Text("Contents Page")
            Spacer()
            Button (action: {
                
                presentedNumbers.append(2)
            })
            {
                HStack {
                    Text("Go to Read")
                }.padding()
                    .foregroundColor(.white)
                    .background(Color.red)
                    .cornerRadius(40)
            }
        }
        
    }
}


struct NavPathTestView_Previews: PreviewProvider {
    static var previews: some View {
        NavPathTestView()
    }
}

Many thanks to lorem-ipsum for their constructive comments.

kelalaka
  • 5,064
  • 5
  • 27
  • 44