首页 > 解决方案 > 使 DisclosureGroup 不可扩展

问题描述

我在另一个 DisclosureGroup 中添加一个 DisclosureGroup 我希望第二个 Disclosure 组不可扩展(删除扩展能力)。这是可能的还是有更好的方法来实现这一目标?

@discardableResult
private func expand(key: String) -> Binding<Bool> {
    return Binding<Bool>(
        get: { expanded.contains(key) },
        set: { isExpanding in
            if isExpanding {
                expanded.insert(key)
            } else {
                expanded.remove(key)
            }
        }
    )
}


DisclosureGroup(isExpanded: expand(key: "Incomplete")) {
    ForEach(Array(array), id: \.self) { key in
        if let innerArray = innerArray {
            // Make non expanable
            DisclosureGroup {
                ForEach(Array(tasks.enumerated()), id:\.1.title) { (index, check) in
                    // Content
                }
            } label: {
                // Header
            }
        }
    }
} label: {
    // Header
}

标签: iosswiftswiftui

解决方案


您可以创建一个常量Binding集为true. 这将使其永久扩展。

如果您希望它永久折叠(不确定为什么要这样),请false改用。

DisclosureGroup(isExpanded: .constant(true)) {
    // Content
} label: {
    // Label
}

推荐阅读