UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 ? 🙄

1      

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())
    }
}

3      

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.

3      

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!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.