I'd love to reuse a list layout between iOS and WatchOS, but InsetGroupedListStyle()
isn't available on WatchOS.
What would be a good way to create a helper that would conditionally return InsetGroupedListStyle()
on iOS and eg. PlainListStyle()
on WatchOS?
I tried this, but get an error that I cannot return ListStyle
(which is probably caused by SwiftUI needing to know a specific type in compile time).
View.swift
List {
// ...
}
.listStyle(MyInsetGroupedListStyle())
Helpers.swift
public func MyInsetGroupedListStyle() -> ListStyle {
#if os(watchOS)
return PlainListStyle()
#else
return InsetGroupedListStyle()
#endif
}
Alternate way could be specifying the listStyle inline, but swift doesn't support conditional compilation in expressions:
View.swift
List {
// ...
}
.listStyle(#if os(watchOS) PlainListStyle() #else InsetGroupedListStyle() #endif)