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

前端 未结 13 1741
盖世英雄少女心
盖世英雄少女心 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:23

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

    I'm not sure if you need all the functionality of "UITableView" in SwiftUI, but if you just want to just display a list of views in iOS 13 or later couldn't you just do:

    ScrollView {
        VStack(alignment: .leading) {
            ForEach(1...10) { _ in
                CustomRow()
            }
        }
    }
    

    And then add .padding() for any margins you want?

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

    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

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

    Remove paddings and separator

    iOS 14.2, Xcode 12.2

    ScrollView {
        LazyVStack {
            ForEach(viewModel.portfolios) { portfolio in
                PortfolioRow(item: portfolio)
            }
        }
    }
    

    This gives you complete control over the list. Current implementation of List doesn't provide full control and contains some issues.

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

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

    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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题