2

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 ?

yuroyami
  • 814
  • 6
  • 24

1 Answers1

2

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")
    }
}
NexusUA
  • 217
  • 1
  • 3
  • 13
udi
  • 3,672
  • 2
  • 12
  • 33
  • So using `Label("Favorites", systemImage: "star")` on pre-15.0 iOS will simply show the star symbol without the fill variant unless I explicitly mention it as "star.fill" ? I don't think I am able to test this currently but I'd like to confirm – yuroyami Apr 06 '23 at 10:53
  • 2
    yes there is an example in the documentation with heart image. check it – udi Apr 06 '23 at 10:57