I have a NavigationDestination (iOS16+) for an enum SideBarTabs. I want to show a different view depending on the actual value of the Type. This works at first:
.navigationDestination(for: SideBarTabs.self) { tab in
tab.sideBarView
.frame(width: getScreenRectangle().width, height: getScreenRectangle().height)
}
(...)
enum SideBarTabs: String, CaseIterable, Hashable {
case about = "About"
case help = "Help"
case contact = "Contact"
case credits = "Credits"
case privacy = "Privacy"
var icon: Image {
switch self {
case .about:
return AppSymbols.SideBar.about
case .help:
return AppSymbols.SideBar.help
case .contact:
return AppSymbols.SideBar.contact
case .credits:
return AppSymbols.SideBar.credits
case .privacy:
return AppSymbols.SideBar.privacy
}
}
var sideBarView: some View {
switch self {
case .about:
return PrivacyView()
case .help:
return PrivacyView()
case .contact:
return PrivacyView()
case .credits:
return PrivacyView()
case .privacy:
return PrivacyView()
}
}
}
BUT
Naturally I want to return a different View per case, so I have to make its return type any View
var sideBarView: any View {
switch self {
case .about:
return AboutView()
case .help:
return HelpView()
case .contact:
return ContactView()
case .credits:
return CreditsView()
case .privacy:
return PrivacyView()
}
But then the calling site (destination) starts complaining...
.navigationDestination(for: SideBarTabs.self) { tab in
tab.sideBarView
.frame(width: getScreenRectangle().width, height: getScreenRectangle().height)
}
...that Type 'any View' cannot conform to 'View'
How do I solve this conundrum, must be easy, right ? 🙄