SwiftUI - 如何在编辑模式下避免列表中的行缩进,但不使用 onDelete?(附上代码/视频) [英] SwiftUI - how to avoid row indenting in List when in Edit Mode, but not using onDelete? (code/video attached)

查看:27
本文介绍了SwiftUI - 如何在编辑模式下避免列表中的行缩进,但不使用 onDelete?(附上代码/视频)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SwiftUI 中,当我进入编辑模式但不使用 onDelete 时,如何避免在我的列表中缩进行内容?这是目前我的行内容在发生这种情况时缩进,好像删除"按钮将显示在左侧,但是我没有使用 onDelete 所以那里没有按钮.

In SwiftUI how can I avoid having row content being indenting in my List when I move into Edit Mode, but not using onDelete? That is currently my row content is indented as this happens, as if the "delete" button will be shown on the left, however I'm not using onDelete so there is no button there.

动画 GIF

此处提取代码:

var body: some View {
    VStack{
        List() {
            ForEach(gcTasks) { gcTask in
                HStack {
                    GCTaskRow(withGcTask: gcTask, haha: "")
                }
            }
            // .onDelete(perform: self.deleteTask)   // NOTE: HAVE REMOVED
            .onMove(perform: self.move)
        }
    }
    .environment(\.editMode, listInEditMode ? .constant(.active) : .constant(.inactive))
}

背景 - 实际上想要始终处于编辑模式,即总是可以选择向上/向下拖动行,但是永远不会使用删除,因此想要查看 onDelete 的所有痕迹,在这种情况下是自动缩进..

Background - Actually want to move to being in EDIT mode always, i.e. so always have the option of dragging row up/down, however will never use Delete hence want to review all traces of onDelete, in this case being the automatic indentation..

更新:另一个例子(操场)是如何发生不需要的缩进的:

UPDATE: Another example (playgrounds) of how the unwanted indenting is occuring:

import SwiftUI
import PlaygroundSupport

let modelData: [String] = ["Eggs", "Milk", "Bread"]

struct ListTestNoDelete: View {
    private func move(from uiStartIndexSet: IndexSet, to uiDestIndex: Int) {
        print("On Move")
    }
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(modelData, id: \.self) { str in
                        Text(str)
                    }
                    .onMove(perform: self.move)
                }
                .environment(\.editMode, .constant(.active))
                .navigationBarTitle( Text("Test") )
            }
        }
    }
}


let listTest = ListTestNoDelete()
PlaygroundPage.current.liveView = UIHostingController(rootView: listTest)

推荐答案

也许不是一个非常优雅的修复,但您可以将列表扩展到左侧.

Maybe not a very elegant fix but you could extend the List to the left.

List {
    // ...
}
// .padding(.leading, self.isEditing == .inactive ? 0 : -39)
.padding(.leading, self.isEditing ? -45 : 0)

这篇关于SwiftUI - 如何在编辑模式下避免列表中的行缩进,但不使用 onDelete?(附上代码/视频)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆