How to remove “row” separators/dividers from a List in SwiftUI?

前端 未结 7 2397
渐次进展
渐次进展 2020-12-05 04:35

I\'m trying to remove the "row" separators (known as dividers in SwiftUI) from a List in SwiftUI.

I went through the List document

相关标签:
7条回答
  • 2020-12-05 04:47

    iOS 13 builds only:

    The current workaround is to remove them via UIAppearance:

    UITableView.appearance(whenContainedInInstancesOf: 
        [UIHostingController<ContentView>.self]
    ).separatorStyle = .none
    
    0 讨论(0)
  • 2020-12-05 04:50

    iOS 14

    Apple introduced LazyVStack In iOS 14. you may consider using it instead of list for this:

    ScrollView {
        LazyVStack {
            ForEach((1...100), id: \.self) {
               Text("Placeholder \($0)")
            }
        }
    }
    

    Keep in mind that LazyVStack is lazy and doesn't render all rows all the time. So they are very performant and suggested by Apple itself in WWDC 2020.


    iOS 13

    There is a UITableView behind SwiftUI's List for iOS. 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() {
        // 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")
        }
    }
    
    0 讨论(0)
  • 2020-12-05 04:50

    iOS 13 builds only:

    See existing answers using UITableView.appearance().

    ⚠️ Be aware that in the iOS 14 SDK, List does not appear to be backed by UITableView. See the alternate solution below:

    iOS 14 Xcode 12 Beta 1 only:

    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.

    ⚠️ This does not work for iOS 13 builds.

    Tested in Xcode 12 beta 1:

    yourRowContent
      .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:

    import 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)
      }
    }
    
    0 讨论(0)
  • 2020-12-05 04:52

    You may use ForEach within a ScrollView instead of List for dynamic views without any styling

    0 讨论(0)
  • 2020-12-05 04:55

    From: Swiftui Views Mastery Book SwiftUI 2.0 Mark Moeykens

    .listStyle(SidebarListStyle()) # IOS 14
    

    You can apply this new list style which will remove the separator lines.

    0 讨论(0)
  • 2020-12-05 05:01

    iOS 13 builds only:

    Adding UITableView.appearance().separatorColor = .clear for initializer

    struct SomeView: View {
      init() {
            UITableView.appearance().separatorColor = .clear
        }
    }
    

    I hope you to resolve this problem.

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