I have stumbled upon a problem that I somehow haven't come across previously. It seems like a simple thing that can be done in SwiftUI that I can't seem to solve.
I would like to be able to switch between GroupedListStyle and InsetGroupedListStyle for compact and regular horizontal size classes, respectively.
In iOS 13, since there was no InsetGroupedListStyle, SwiftUI automatically switched between the two on change of horizontal size class, but this feature has been removed. I'd like to have thought a replacement for this had been implemented but supposedly not (that I know of).
I thought that applying
I tried then extracting it to a variable:
Naively, I added @ViewBuilder to see if that would work until realising these weren't views being returned.
There is no type erased list style (AnyListStyle) so that can't be applied in this situation.
I thought about making a custom ListStyle but the protocol requirements put me off. Even a custom ViewModifier didn't work.
Is this even possible in SwiftUI, or am I missing something obvious?
I would like to be able to switch between GroupedListStyle and InsetGroupedListStyle for compact and regular horizontal size classes, respectively.
In iOS 13, since there was no InsetGroupedListStyle, SwiftUI automatically switched between the two on change of horizontal size class, but this feature has been removed. I'd like to have thought a replacement for this had been implemented but supposedly not (that I know of).
I thought that applying
Code Block Swift .listStyle(horizontalSizeClass == .compact ? GroupedListStyle() : InsetGroupedListStyle())
would work, but apparently not. Although they both conform to ListStyle, Xcode says they have mismatching types.I tried then extracting it to a variable:
Code Block Swift private var listStyle: some ListStyle { if horizontalSizeClass == .compact { return GroupedListStyle() } else { return InsetGroupedListStyle() } }
That didn't work resulting in the same error. Naively, I added @ViewBuilder to see if that would work until realising these weren't views being returned.
There is no type erased list style (AnyListStyle) so that can't be applied in this situation.
I thought about making a custom ListStyle but the protocol requirements put me off. Even a custom ViewModifier didn't work.
Is this even possible in SwiftUI, or am I missing something obvious?