Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created December 30, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StanislavK/eb1e4dd0e2ebff66be80e201ec11b3c2 to your computer and use it in GitHub Desktop.
Save StanislavK/eb1e4dd0e2ebff66be80e201ec11b3c2 to your computer and use it in GitHub Desktop.
Single line list row separator (edge to edge).
import SwiftUI
struct Row: View {
let text: String
let showDivider: Bool
var body: some View {
VStack(alignment: .leading) {
Text(text)
.padding(showDivider ? [.leading, .top] : [.leading, .top, .bottom])
if showDivider {
Divider().background(Color(UIColor.separator))
}
}
}
}
struct ContentView: View {
enum Flavor: String, CaseIterable, Identifiable {
var id: String { self.rawValue }
case vanilla, chocolate, strawberry
}
var body: some View {
List {
ForEach(Flavor.allCases, id: \.self) { flavor in
Row(text: "\(flavor.rawValue)", showDivider: flavor == Flavor.allCases.last ? false : true)
}
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
.listRowBackground(Color(UIColor.systemYellow).opacity(0.2))
}
.listStyle(InsetGroupedListStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@StanislavK
Copy link
Author

This will create the following list with single line style separators:

Screenshot 2021-12-30 at 18 43 22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment