HStack 中的 SwiftUI EditButton 未激活编辑模式 [英] SwiftUI EditButton in HStack not activating edit mode

查看:32
本文介绍了HStack 中的 SwiftUI EditButton 未激活编辑模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为更大表单的一部分,我希望有一个带有列表的部分,其条目可以重新排序.

在整个视图中,应该只能编辑该特定列表,不能进行其他操作.因此,我希望将 EditButton 放在列表附近.

如果我只是将 EditButton 设置为部分标题,则重新排序列表项会起作用,但我无法显示部分标题:

struct ContentView: 查看 {private let items = Range(1...4).map { "Item "+ 字符串($0) }var主体:一些视图{形式 {部分(标题:EditButton()){ForEach(items, id: \.self) { item in文本(项目)}.onMove(执行:reorderItems).onDelete(执行:deleteItems)}}}func reorderItems(from sourceIndices: IndexSet, to destinationIndex: Int) {/* ... */}func deleteItems(at offsets: IndexSet) {/* ... */}}

但是,如果我将 EditButton 包装在 HStack 中以显示右侧的按钮,则点击编辑"即可.将按钮的标题更改为完成";但不再启动 List 的编辑模式:

struct ContentView: 查看 {@Environment(\.editMode) var editModeprivate let items = Range(1...4).map { "Item "+ 字符串($0) }var主体:一些视图{形式 {部分(标题:HStack {文本(章节标题")垫片()编辑按钮()}.environment(\.editMode, self.editMode)) {ForEach(items, id: \.self) { item in文本(项目)}.onMove(执行:reorderItems).onDelete(执行:deleteItems)}}}func reorderItems(from sourceIndices: IndexSet, to destinationIndex: Int) {/* ... */}func deleteItems(at offsets: IndexSet) {/* ... */}}

我也尝试过,如代码所示,将 editMode 环境变量传递给 HStack,但没有任何帮助.

有没有办法让 HStack 中的 EditButton 工作?

(备注:由于 List 是更大表单的一部分,因此建议将 EditButton 放在 Section 之外

部分(标题:EditButton().frame(maxWidth: .infinity, alignment: .trailing).overlay(Text(Header"), 对齐方式:.leading)){ForEach(items, id: \.self) { item in文本(项目)}.onMove(执行:reorderItems).onDelete(执行:deleteItems)}

As part of a bigger Form I'd like to have a Section with a List whose entries can be reordered.

In the whole view it should be only possible to edit that particular list, nothing else. Therefore I'd like to have the EditButton near the List.

If I just set the EditButton as the Section header, reordering List items works but I can't display a section title:

struct ContentView: View {
    
    private let items = Range(1...4).map { "Item " + String($0) }
    
    var body: some View {
        Form {
            Section(header: EditButton()) {
                ForEach(items, id: \.self) { item in
                    Text(item)
                }
                .onMove(perform: reorderItems)
                .onDelete(perform: deleteItems)
            }
        }
    }

    func reorderItems(from sourceIndices: IndexSet, to destinationIndex: Int) { /* ... */ }

    func deleteItems(at offsets: IndexSet) { /* ... */ }
}

But if I wrap the EditButton in a HStack to display the button on the right, a tap on "Edit" changes the button's title to "Done" but doesn't start List's edit mode anymore:

struct ContentView: View {

    @Environment(\.editMode) var editMode
    
    private let items = Range(1...4).map { "Item " + String($0) }
    
    var body: some View {
        Form {
            Section(header: HStack {
                Text("Section title")
                
                Spacer()
                
                EditButton()
            }.environment(\.editMode, self.editMode)) {
                ForEach(items, id: \.self) { item in
                    Text(item)
                }
                .onMove(perform: reorderItems)
                .onDelete(perform: deleteItems)
            }
        }
    }

    func reorderItems(from sourceIndices: IndexSet, to destinationIndex: Int) { /* ... */ }

    func deleteItems(at offsets: IndexSet) { /* ... */ }
}

I also tried, as the code shows, passing the editMode environment variable to the HStack, but nothing helped.

Is there any way to get the EditButton inside an HStack working?

(Remark: As the List is part of a bigger Form, placing the EditButton out of the Section as suggested here is not an option in my case.)

解决方案

Here is working solution - looks like they require that EditButton was a root view of section, so we can construct everything else above it. (tested with Xcode 11.4 / iOS 13.4)

Note: @Environment(\.editMode) var editMode is not needed

Section(header:
    EditButton().frame(maxWidth: .infinity, alignment: .trailing)
        .overlay(Text("Header"), alignment: .leading)
)
{
    ForEach(items, id: \.self) { item in
        Text(item)
    }
    .onMove(perform: reorderItems)
    .onDelete(perform: deleteItems)
}

这篇关于HStack 中的 SwiftUI EditButton 未激活编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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