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

前端 未结 7 2398
渐次进展
渐次进展 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 05:06

    iOS 13 builds only

    while this solution works correctly let's clean up the work using ViewModifier

    public struct ListSeparatorStyleNoneModifier: ViewModifier {
        public func body(content: Content) -> some View {
            content.onAppear {
                UITableView.appearance().separatorStyle = .none
            }.onDisappear {
                UITableView.appearance().separatorStyle = .singleLine
            }
        }
    }
    

    now let's make a small extension that would help to hide the details

    extension View {
        public func listSeparatorStyleNone() -> some View {
            modifier(ListSeparatorStyleNoneModifier())
        }
    }
    

    As you can see, we’ve wrapped our appearance setting code into a neat little view modifier. you can declare it directly now

    List {
        Text("1")
        Text("2")
        Text("3")
    }.listSeparatorStyleNone()
    
    0 讨论(0)
提交回复
热议问题