Search code examples
iosswiftxcodeswiftuiswiftui-tabview

Making a symbol image not filled on iOS versions below 15.0


I have a TabView with labels, I am not sure why this isn't documented at all and I've found on another question on the stackoverflow that the following lines of code only works on iOS 15.0:

    if (selectedTab == 2) {
        Label("Playlist", systemImage: "star.fill")
    } else {
        Label("Playlist", systemImage: "star")
            .environment(\.symbolVariants, .none)
    }

This works as desired when my project has 15.0 as the minimum version, however, anything below marks the following error:

Key path value type 'WritableKeyPath<EnvironmentValues, SymbolVariants>' cannot be converted to contextual type 'KeyPath<EnvironmentValues, SymbolVariants>

How do I make earlier versions show unfilled icons (outline/default/none) in a TabView ?


Solution

  • As far as I understand it is the expected behaviour and it is documented.

    Check the documentation here. According to it SymbolVariants only available in iOS 15 + versions. So ur code will only work project has 15.0 as the minimum version and it will give the above error when a lower version is used.

    What u can do is check the versions and add separate codes as below.

    if #available(iOS 15.0, *) {
        if selectedTab == 2 {
            Label("Favorites", systemImage: "star.fill")
        } else {
            Label("Favorites", systemImage: "star")
                .environment(\.symbolVariants, .none)
        }
    } else {
        if selectedTab == 2 {
            Label("Favorites", systemImage: "star.fill")
        } else {
            Label("Favorites", systemImage: "star")
        }
    }