NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: NavigationStack (iOS16+), destination from enum Type, cannot conform to View

Forums > SwiftUI

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 ? πŸ™„

   

I'm not sure about this, but does making the switch return AnyView instances help at all?

var sideBarView: some View {
    switch self {
    case .about:
        return AnyView(AboutView())
    case .help:
        return AnyView(HelpView())
    case .contact:
        return AnyView(ContactView())
    case .credits:
        return AnyView(CreditsView())
    case .privacy:
        return AnyView(PrivacyView())
    }
}

2      

Another option, to not use AnyView would be to wrap the entire switch in a Group :

var sideBarView: some View {
  Group {
    switch self {
    case .about:
        AboutView()
    case .help:
        HelpView()
    case .contact:
        ContactView()
    case .credits:
        CreditsView()
    case .privacy:
        PrivacyView()
    }
  }
}

or - if you want to avoid wrapping your views in any way - you can annotate your property with @ViewBuilder (that's how var body: some View actually works in View protocol) :

@ViewBuilder var sideBarView: some View {
    switch self {
    case .about:
        AboutView()
    case .help:
        HelpView()
    case .contact:
        ContactView()
    case .credits:
        CreditsView()
    case .privacy:
        PrivacyView()
    }
}

Notice that in both cases the return keyword is dropped from the switch cases - that's because in @ViewBuilder context (either explicit, or in the Group) the entire switch is actually "converted" to a conditional view under the hood.

2      

Thank you both! Been away a few days, so a bit late to react.

I use @Viewbuilder a lot in my views, but it never occurred to me to use it in my enum. Silly me! That was the solution to my problem!

1      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.