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]
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))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.