如何删除SwiftUI中最后一个List项目中的分隔线?

时间:2020-05-14 13:52:53

标签: swiftui swiftui-list

这是我的代码,我尝试将NavigationLinks用作某种菜单:

struct ContentView: View {

    init() {
        UITableView.appearance().tableFooterView = UIView()
    }

    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: Text("Test")) {
                    Text("Link A")
                }
                NavigationLink(destination: Text("Test")) {
                    Text("Link B")
                }
                NavigationLink(destination: Text("Test")) {
                    Text("Link C")
                }
                Text("Footer content here")
            }
        }
    }
}

它是这样的: enter image description here

是否可以删除最底部一项上的Divider,即位于“内容在这里”下方的行?

1 个答案:

答案 0 :(得分:1)

这是可能的解决方案。使用Xcode 11.4 / iOS 13.4进行了测试

demo

extension View {
    func listRowUpperSeparator() -> some View {
        self.listRowBackground(
            VStack {
                Divider().padding(.leading)
                Spacer()
            })
    }
}

struct ContentView: View {

    init() {
        UITableView.appearance().separatorStyle = .none
    }

    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: Text("Test")) {
                    Text("Link A")
                }.listRowUpperSeparator()
                NavigationLink(destination: Text("Test")) {
                    Text("Link B")
                }.listRowUpperSeparator()
                NavigationLink(destination: Text("Test")) {
                    Text("Link C")
                }.listRowUpperSeparator()
                Text("Footer content here")
                    .listRowUpperSeparator()
            }
        }
    }
}