【问题标题】:SwiftUI Custom List Cell - disable horizontal padding/shrink in EditModeSwiftUI 自定义列表单元格 - 在 EditMode 中禁用水平填充/收缩
【发布时间】:2023-02-08 07:24:25
【问题描述】:

我正在尝试在 SwiftUI 中创建一个自定义列表单元格,其中编辑模式下的拖动图标保留在单元格内。

默认情况下,一旦列表进入编辑模式,单元格就会水平收缩,为拖动手柄和删除按钮腾出空间。

实际上添加 listRowBackground 可以解决问题,但是我无法再添加 cornerRadius 和 padding 了。

现在发生了什么:

期望的行为:

有谁知道内省如何实现这一目标的技巧或解决方案?

示例代码:

struct ListInList: View {

    @State var datas = ["Row 1", "Row 2", "Row 3"]

    var body: some View {
        NavigationView{
            List{
            
                ForEach(datas, id: \.self) { data in
                    HStack{
                        Text(data)
                            .frame(maxWidth: .infinity)
                            .padding()
                    }
                    .listRowSeparator(.hidden)
                    .listRowInsets(EdgeInsets())
                    .listRowBackground(Color.clear)
                    .ignoresSafeArea(edges: .horizontal)
                    .background(Color.gray.opacity(0.3))
                    .cornerRadius(10)
                    .deleteDisabled(true)
                    .padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
                }
                .onMove(perform: move)
            }
            .listStyle(.plain)
            .toolbar{
                ToolbarItem(placement: .navigationBarTrailing){
                    EditButton()
                }
            }
        }
    }

    func move(from source: IndexSet, to destination: Int) {
        datas.move(fromOffsets: source, toOffset: destination)
    }
}

【问题讨论】:

    标签: ios swift xcode uitableview swiftui


    【解决方案1】:

    好的,我自己弄清楚了。 我完全忘记了 .listRowBackground() 可以接收视图。

    所以对于每个面临同样问题的人:

        List{
            ForEach(datas, id: .self) { data in
                HStack{
                    Text(data)
                        .padding(.vertical,20)
                }
                .listRowSeparator(.hidden)
                .listRowBackground(
                    Color.gray.opacity(0.3)
                        .cornerRadius(10)
                        .padding(.vertical, 8)
                    )
            }
            .onMove(perform: move)
        }
        .padding(.horizontal)
        .listStyle(.plain)
    

    【讨论】:

      【解决方案2】:

      我认为你应该使用 .insetGrouped 列表样式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 2018-09-24
        相关资源
        最近更新 更多