2
votes

Since SwiftUI's NavigationView is extremely rigid and does not provide an easy way for programatic navigation, I wanted to create an AppNavigationView that has an array of views and updates its rendered view based on push/pop.

Because a SwiftUI view is a protocol with an associatedType, I cannot either create an array of Views or even pass a few in my public func pushView(view:View).

So how can I store a list of View structs?

// Error: Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements.
@State var navigationStack: [View]
1

1 Answers

3
votes

Swift does not allow to use protocol as member type, so most close would be to use

@State private var navigationStack: [AnyView]

//...

public func pushView<V:View>(view: V) {
   navigationStack.append(AnyView(view))
}