Member-only story
Create an Inset Grouped List in SwiftUI for macOS
When you have an iOS app, especially when it displays information, you maybe use InsetGroupedListStyle
on your List
. This looks any like this:
But there is a problem, when you want to display this view on macOS. InsetGroupedListStyle
is unavailable for native apps (It’s available for Mac Catalyst though). And when you use alternative list styles for the list, it looks like this:
It doesn’t look that bad, but it is not consistent with the iOS app design.
Creating the view extension
There isn’t a way to create a custom ListStyle
. So we create a view extension
, that we can use on the sections.
So that we have different code for iOS and macOS, we can use an #if
closure. The header parameter has a view as input, and is placed above the header. For the iOS section, we can just use a regular section:
self
represents the view you’re using the method on. You can replace the // iOS
with this bit of code. But macOS isn’t much more complicated at all. GroupBox
fulfills nearly all of our expectations.