I do have a pure SwiftUI solution for iOS 14, but who knows how long it's going to continue working for. It relies on your content being the same size (or larger) than the default list row and having an opaque background.
Tested in Xcode 12 beta 1:
Code Block swiftyourRowContent |
.padding(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)) |
.frame( |
minWidth: 0, maxWidth: .infinity, |
minHeight: 44, |
alignment: .leading |
) |
.listRowInsets(EdgeInsets()) |
.background(Color.white) |
Or if you're looking for a reusable ViewModifier:
Code Block swiftimport SwiftUI |
|
struct HideRowSeparatorModifier: ViewModifier { |
|
static let defaultListRowHeight: CGFloat = 44 |
|
var insets: EdgeInsets |
var background: Color |
|
init(insets: EdgeInsets, background: Color) { |
self.insets = insets |
|
var alpha: CGFloat = 0 |
UIColor(background).getWhite(nil, alpha: &alpha) |
assert(alpha == 1, "Setting background to a non-opaque color will result in separators remaining visible.") |
self.background = background |
} |
|
func body(content: Content) -> some View { |
content |
.padding(insets) |
.frame( |
minWidth: 0, maxWidth: .infinity, |
minHeight: Self.defaultListRowHeight, |
alignment: .leading |
) |
.listRowInsets(EdgeInsets()) |
.background(background) |
} |
} |
|
extension EdgeInsets { |
|
static let defaultListRowInsets = Self(top: 0, leading: 16, bottom: 0, trailing: 16) |
} |
|
extension View { |
|
func hideRowSeparator( |
insets: EdgeInsets = .defaultListRowInsets, |
background: Color = .white |
) -> some View { |
modifier(HideRowSeparatorModifier( |
insets: insets, |
background: background |
)) |
} |
} |
|
struct HideRowSeparator_Previews: PreviewProvider { |
|
static var previews: some View { |
List { |
ForEach(0..<10) { _ in |
Text("Text") |
.hideRowSeparator() |
} |
} |
.previewLayout(.sizeThatFits) |
} |
} |