How to remove the line separators from a List in SwiftUI without using ForEach?

前端 未结 13 1746
盖世英雄少女心
盖世英雄少女心 2021-01-30 04:50

I have this code to display a list of custom rows.

struct ContentView : View {
    var body: some View {
        VStack(alignment: .leading) {
            List(1         


        
相关标签:
13条回答
  • 2021-01-30 05:40

    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.

    0 讨论(0)
  • 2021-01-30 05:41

    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
    
    0 讨论(0)
  • 2021-01-30 05:42

    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
    }
    
    0 讨论(0)
  • 2021-01-30 05:43

    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

    0 讨论(0)
  • 2021-01-30 05:43

    For iOS 14 you have this :

    .listStyle(SidebarListStyle()) # IOS 14
    
    0 讨论(0)
  • 2021-01-30 05:48

    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.

    0 讨论(0)
提交回复
热议问题