问题
I have this code to display a list of custom rows.
struct ContentView : View {
var body: some View {
VStack(alignment: .leading) {
List(1...10) {_ in
CustomRow()
}
}
}
}
However, I want to remove the line on each row. I tried not using List and instead using ForEach inside ScrollView but it completely removes all the styling including its padding and margins. I just want to remove the lines and nothing else.
Please help, thank you.
回答1:
iOS 14:
you may consider using a LazyVStack inside a ScrollView instead.
iOS 13:
There is a UITableView behind SwiftUI's List for iOS 13. So to remove
Extra separators (below the list):
you need a tableFooterView and to remove
All separators (including the actual ones):
you need separatorStyle to be .none
init() {
if #available(iOS 14.0, *) {
// iOS 14 doesn't have extra separators below the list by default.
} else {
// To remove only extra separators below the list:
UITableView.appearance().tableFooterView = UIView()
}
// To remove all separators including the actual ones:
UITableView.appearance().separatorStyle = .none
}
var body: some View {
List {
Text("Item 1")
Text("Item 2")
Text("Item 3")
}
}
Note that a static list doesn't show extra separators below the list by default
回答2:
iOS 13 builds only:
Adding UITableView.appearance().separatorColor = .clear anywhere in your code before the List appears should work. While this solution removes the separators, note that all List instances will be bound to this style as there’s no official way currently to only remove separators of specific instances. You may be able to run this code in onAppear and undo it in onDisappear to keep styles different.
Also note that this code assumes Apple is using a UITableView to back List which is not true in the iOS 14 SDK. Hopefully they add an official API in the future. Credit to https://twitter.com/singy/status/1169269782400647168.
回答3:
Check out SwiftUI-Introspect. It exposes the underlying UIKit/AppKit views.
iOS 13 builds only:
In this case you could manipulate the UITableView directly (without having to change all table views via the appearance proxy):
import Introspect
:
:
List {
...
}.introspectTableView { tableView in
tableView.separatorStyle = .none
}
回答4:
iOS 13 builds only:
While these answers are technically correct they will affect a List or Form globally(across the entire app) from my experience.
A hacky way I found to resolve this problem, at least in my app, is to add the following code to the "main" content view of the app:
.onAppear(perform: {
UITableView.appearance().separatorStyle = .none
})
Then on any other view that you want to the separator lines add this to the end of the List or Form view
.onAppear(perform: {
UITableView.appearance().separatorStyle = .singleLine
})
This seems to add the single line separator to any view sheet that is above the main content view. Again this is all anecdotal to my recent SwiftUI experience.
In my experience I only had to add the .onAppear(... = .singleLine) method to one of my "detail" views and the separator line appeared on all subsequent views that were presented.
Edit: Another note as this answer continues to gain attention. This solution I posted doesn't solve all cases, it certainly did not solve it for me, again in some cases. I ended up using Introspect for SwiftUI to solve this problem across the entire app.
I hope this clears up some confusion and frustration as people come across this post.
回答5:
Doing something like:
UITableView.appearance().separatorColor = .clear
works, but in a lot of cases is not something that I would recommend. These are global changes - i.e. they will affect all instances of UITableView. This is a problem if you have multiple UITableViews that want different styles. Or if you are developing a framework, clients using your framework will inherit those changes too!
A safer solution is to only target UITableViews that live inside a specified container. Luckily the appearance api gives us a way to be specific:
UITableView.appearance(whenContainedInInstancesOf: [UIHostingController<YourSwiftUiViewHere>.self]).separatorColor = .clear
回答6:
IOS 14
There is currently no solution to hide the separators on the IOS 14 beta.
If you don't need an editable List, you should use a LazyVStack inside a ScrollView.
But if you want to stay on the List. I found a solution on the Apple forum by samwarner. https://developer.apple.com/forums/thread/651028
This is a temporary solution. In some cases you may need to adjust the insets. Here is its implementation:
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))
}
}
Finally, here is the implementation on a list. You have to add .hideRowSeparator() on the list cell.
struct CustomRow: View {
let text: String
var body: some View {
HStack {
Text(self.text)
Image(systemName: "star")
}
}
}
struct ContentView : View {
@State private var fruits = ["Apple", "Orange", "Pear", "Lemon"]
var body: some View {
VStack(alignment: .leading) {
List {
ForEach(self.fruits, id: \.self) { str in
CustomRow(text: str)
.hideRowSeparator()
}
}
}
.padding(.top)
}
}
回答7:
Use a ScrollView?
Some state that represents your list
@State var menuItems: [String] = ["One", "Two", "Three"]
The a SwiftUI ForEach loop inside a ScrollView
ScrollView {
ForEach(self.menuItems, id: \.self) { item in
Text(item)
}
}
回答8:
I started a project to solve this in iOS14 since the iOS 13 workarounds no longer work. It allows setting the separator style, separator color, and separator inset.
Hide separators on the List
List { <content> }
.listSeparatorStyle(.none)
Show a single divider line with configurable color and insets
List { <content> }
.listSeparatorStyle(.singleLine, color: .red, inset: EdgeInsets(top: 0, leading: 50, bottom: 0, trailing: 20)
https://github.com/SchmidtyApps/SwiftUIListSeparator
回答9:
All the answers tell you to use ScrollView (which is what I recommend too)
But in case you want to use List and want to remove the separator lines..
Install the SwiftPM: https://github.com/siteline/SwiftUI-Introspect
SAMPLE:
List {
Text("Item 1")
Text("Item 2")
}
.introspectTableView { tableView in
tableView.separatorStyle = .none
}
回答10:
This is my extension ListRowExtensions for hide list row separator and custom this one.
import SwiftUI
// MARK: List row extensions
extension View {
func hideListRowSeparator() -> some View {
return customListRowSeparator(insets: .init(), insetsColor: .clear)
}
func customListRowSeparator(
insets: EdgeInsets,
insetsColor: Color) -> some View {
modifier(HideRowSeparatorModifier(insets: insets,
background: insetsColor
)) .onAppear {
UITableView.appearance().separatorStyle = .none
UITableView.appearance().separatorColor = .clear
}
}
}
// MARK: ViewModifier
private struct HideRowSeparatorModifier: ViewModifier {
var insets: EdgeInsets
var background: Color
func body(content: Content) -> some View {
content
.padding(insets)
.frame(
minWidth: 0,
maxWidth: .infinity,
maxHeight: .infinity,
alignment: .leading
)
.listRowInsets(EdgeInsets())
.background(background)
}
}
Use :
// Without list row separator
List {
ForEach(self.viewModel.data, id: \.id) { item in
Text("Text")
}
.hideRowSeparatorItemList()
}
// With list row separator with color and size
List {
ForEach(self.viewModel.data, id: \.id) { item in
Text("Text")
}
.customListRowSeparator(insets: EdgeInsets(top: 0,
leading: 0,
bottom: 5,
trailing: 0),
insetsColor: Color.red)
}
来源:https://stackoverflow.com/questions/56553672/how-to-remove-the-line-separators-from-a-list-in-swiftui-without-using-foreach