13

有没有办法List在 SwiftUI 中删除分隔符或调整分隔符插入?

在 UIKit 中可以通过

tableView.separatorStyle = .none

tableview.separatorInset = UIEdgeInsets(top: 0, left: 18, bottom: 0, right: 18)

对应的 SwiftUI 替代方案是什么?

4

5 回答 5

5

对于 ios 13 不适用于 ios 14

您可以使用以下方法删除分隔符: UITableView.appearance().separatorStyle = .none 在 SwiftUI

加个就行

List() {
}.onAppear {
         UITableView.appearance().separatorColor = .clear
}

或者

struct SomeListView : View {
    init( ) {          
        UITableView.appearance().separatorStyle = .none
           }

    var body : some View {
        Text("TEST")
        }

struct CallList : View {
    var body : some View {

    List() {
    SomeListView()
    }
        }
}
于 2019-11-02T20:04:54.790 回答
3

iOS 15.0+

Mac 催化剂 15.0+

listRowSeparator(_:edges:)

设置与此特定行关联的分隔符的显示模式。 https://developer.apple.com/

List {
    ForEach(0..<10, id: \.self) { number in
        Text("Text\(number)")
    }.listRowSeparator(.hidden)
}


iOS 2.0+

struct ListRowSeperatorModifier: ViewModifier {
    func body(content: Content) -> some View {
        if #available(iOS 15.0, *) {
            content.listRowSeparator(.hidden)
        } else {
            content.onAppear {
                UITableView.appearance().separatorStyle = .none
            }
            .onDisappear {
                UITableView.appearance().separatorStyle = .singleLine
            }
        }
    }
}


extension View {
    func hideListRowSeparator() -> some View  {
        return  self.modifier(ListRowSeperatorModifier())
    }
}

.hideListRowSeparator()上使用ForEach

List {
    ForEach(0..<10, id: \.self) { number in
       Text("Text\(number)")
    }.hideListRowSeparator()
}

于 2021-06-11T11:29:18.443 回答
2

这一切都可以在 SwiftUI 中完成

与 UIKit 中的旧方法相比,要删除分隔符,请tableView.separatorStyle = .none在 init 或表格视图的 onAppear 方法中添加以下行:

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

要与 UIKit 中的行相比调整分隔符插入,请tableview.separatorInset = UIEdgeInsets(top: 0, left: 18, bottom: 0, right: 18)在 init 或 onAppear 方法中添加此行:

List(...){
    ...
}.onAppear() {
    UITableView.appearance().separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 50)
}
于 2020-02-22T19:21:35.217 回答
-2

SwiftUI 中

删除分隔符

 init() {

    UITableView.appearance().separatorStyle = .none //remove separators

 }

 var body: some View {

    List {

        Text("Index 1")
        Text("Index 2")
        Text("Index 3")
        Text("Index 4")
    }

 }
于 2020-01-07T11:41:49.127 回答
-5

对于后者,您可以使用listRowInsets

List { 
  Text("Item 1")
  Text("Item 2")
  Text("Item 3")
}
.listRowInsets(EdgeInsets(top: 0, left: 18, bottom: 0, right: 18))

于 2019-06-08T12:09:55.883 回答