title: How do I add Animations to Transitons between custom NavigationItems made from AnyView?
body: <p>Is there a way to add animations to transitions between custom <code>NavigationItem</code>s made from <code>AnyView</code> as described in <a href="https://medium.com/swlh/swiftui-custom-navigation-view-for-your-applications-7f6effa7dbcf" rel="nofollow noreferrer">this article</a>?</p>
  
  <p>I like everything about this <code>NavigationStack</code> system except for not being able to add any animated transitions.</p>
  
  <p>I understand the problem has to do with type-erasing <code>some View</code>s with <code>AnyView</code>, as described in <a href="https://stackoverflow.com/questions/58160011/swiftui-add-subview-dynamically-but-the-animation-doesnt-work">this answer</a>, and that <code>Group</code> seems to be a better choice for animating custom view navigations. </p>
  
  <blockquote>
    <p>Rather that using AnyView and type-erasure, I prefer to encapsulate the conditional logic inside of a Group view. Then the type you return is Group, which will animate properly.</p>
  </blockquote>
  
  <p>I've run out of ideas and I need some help.</p>
  
  <p>I'll add my code for context.</p>
  
  <p>SceneDelegate:</p>
  
  <pre><code>class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  ...
      let contentView = ContentView().environmentObject(UserData())
  ...
  }
  </code></pre>
  
  <p>ContentView: </p>
  
  <pre><code>struct ContentView: View {
      @EnvironmentObject var userData: UserData   
      var body: some View {
          NavigationHost()
              .environmentObject(NavigationStack(
                  NavigationItem(view: AnyView(
                      (userData.isSignedIn ? AnyView(HomeView()) : AnyView(RegisterView1(profile: Profile.default)) )
                      .transition(.move(edge: .trailing))
                      .animation(Animation.linear(duration: 1))))))
                      //this animation works for some reason, but only this one.
              .environmentObject(UserData())
      }
  }
  </code></pre>
  
  <p>NavigationHost:</p>
  
  <pre><code>struct NavigationHost: View{
      @EnvironmentObject var navigation: NavigationStack
      @EnvironmentObject var userData: UserData
  
      var body: some View {
              ZStack {
                      self.navigation.currentView.view
                          .environmentObject(self.userData)
                          .environmentObject(self.navigation)
                      ...
                  }
              }
      }
  }
  </code></pre>
  
  <p>NavigationStack:</p>
  
  <pre><code>final class NavigationStack: ObservableObject {
      @Published var viewStack: [NavigationItem] = []
      @Published var currentView: NavigationItem
  
      init(_ currentView: NavigationItem ){
          print("Navigation Stack Initialized")
          self.currentView = currentView
      }
  
      func back() {
          if viewStack.count == 0 {
              return
          }
          let last = viewStack.count - 1
          currentView = viewStack[last]
          viewStack.remove(at: last)
      }
  
      navigation.advance(NavigationItem(AnyView))
      func advance(_ view: NavigationItem) {
          viewStack.append( currentView )
          currentView = view
      }
  
      func home() {
         currentView = NavigationItem( view: AnyView(HomeView()) )
         viewStack.removeAll()
      }
  
  }
  
  struct NavigationItem{
      var view: AnyView
  }
  
  </code></pre>