2

This seems like maybe a simple question, but I'm looking for a way to make one (or more, I suppose) buttons in an ActionSheet in SwiftUI to be disabled. For example, if something is illegal based on the state of the app, I don't want to let the user click the button - indeed, I might change the text to explain why it's disabled.

Xcode autocomplete isn't really turning anything up for me, and the docs (here and here) aren't giving me anything. Is my best/only option simply not putting the button in the list of buttons in the ActionSheet? (And kinda letting the user infer why it's not there...)

Thanks so much for any advice!

My simple code:

  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)
  }
aheze
  • 24,434
  • 8
  • 68
  • 125
Hoopes
  • 3,943
  • 4
  • 44
  • 60

1 Answers1

1

For nondestructive actions you can simply show a Menu. They support the .disabled(true) modifier.

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