2

I have a confirmationDialog with a few buttons. In some states, a given button may not do anything useful. I can make the button disappear with...

MyButton.disabled(disableFlag)

...but this changes the dialog layout. IMHO, the dialog looks better (more consistent, familiar) with greyed out buttons.

At first I was hoping for something like this...

Button("Text", role: .disabled) { ... }

...but this won't switch.

I could set the text colour by hand, and disable what the button does. Making the text colour switch on a condition seems tricky but possible. But I am the only person developing my app, and I would much rather use some existing solution if there is one.

Am I missing something? Is there a nice way to do this?

I am using Xcode 14.1 compiling for an iPhone 12 mini running iOS 16.0. My dialog code looks like this...

    .confirmationDialog("White reference", isPresented: $showWhiteDialog) {
         Button("Set default white") {
             // (do stuff deleted)
         }.disabled(customWhite == false)
         Button("Set custom white from swatch") {
             // (do stuff deleted)
         }          
         Button("Fit swatch to white") {
             // (do stuff deleted)
         }.disabled(customWhite == false)
    }
4
  • Actually disabled(true) (unlike hidden()) shows the UI element greyed out.
    – vadian
    Commented Dec 2, 2022 at 13:58
  • Thanks. But my button disappears. I have added schematic code and my build details. I will have a look to see whether disabled() can take other parameters. Commented Dec 2, 2022 at 14:16
  • Sorry, my bad, I overlooked ConfirmationDialog. Those buttons are not customizable.
    – vadian
    Commented Dec 2, 2022 at 16:08
  • Thanks anyhow. That is the bit I was missing. I can do it by hand if I feel keen enough. Commented Dec 3, 2022 at 19:14

1 Answer 1

1

vadian provided the actual answer - you can't.

The real answer is not to use a Conditional Dialog, but to customise your own. If you request 'do a thing', the Conditional Dialog is the 'are you sure?' confirmation, listing possible other actions, and always including 'Cancel'. Put like this, doers does not seem to be a place for greyed out buttons.

IMHO, my use fitted this model. Adding greyed out-options meant you kept the look and feel but the dialog always had the same layout, so the user recognised it.

1
  • Apple is brilliant. They deprecated ActionSheets that allowed any customizations at all and replaced it with ConfirmationDialog that is exactly the same. Apple have to start planning their APIs and stop this deprecation stupidity just because they feel that the naming in the API is not exactly right.
    – Duck
    Commented Oct 19, 2023 at 13:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.