【问题标题】:SwiftUI ActionSheet disabled buttonSwiftUI ActionSheet 禁用按钮
【发布时间】:2023-04-04 16:16:02
【问题描述】:

这似乎是一个简单的问题,但我正在寻找一种方法来禁用 SwiftUI 中 ActionSheet 中的一个(或多个,我想是)按钮。例如,如果某项基于应用程序的状态是非法的,我不想让用户点击按钮 - 实际上,我可能会更改文本来解释它被禁用的原因。

Xcode 自动完成功能并没有真正为我带来任何好处,文档(herehere)也没有给我任何帮助。我最好/唯一的选择是根本不把按钮放在ActionSheet 的按钮列表中吗? (有点让用户推断它为什么不存在......)

非常感谢您的建议!

我的简单代码:

  private func getActionSheet() -> ActionSheet {

    let buttons: [ActionSheet.Button] = [
      .default(Text("This is fine...")) { foo() },
      .default(Text("This is forbidden - disable me!")) { foo() },
      .cancel()
    ]

    return ActionSheet(title: Text("Do it!"), buttons: buttons)
  }

【问题讨论】:

  • 操作表没有禁用功能。为什么不只显示自定义视图?还是菜单?

标签: ios swift swiftui swiftui-actionsheet


【解决方案1】:

对于非破坏性操作,您可以简单地显示一个菜单。他们支持.disabled(true) 修饰符。

 var body: some View {
    Menu("Do it!") {
        Button("This button works fine", action: { })
        
        Button("This one is disabled", action: { })
            .disabled(true)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2021-06-12
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多