How set inactive a button in SwiftUI

Disable a SwiftUI button so user cannot tap it.

SOFTWARE DEVELOPMENTIOS DEVELOPMENTSWIFTUI

Eduardo Domínguez Menéndez

7/22/20241 min read

Despite its utility, the .disabled modifier can sometimes be a little hidden within SwiftUI’s extensive list of modifiers. Let's explore it!

By default, buttons in SwiftUI are always tappable, but sometimes you may need to disable them based on certain conditions. The .disabled modifier has this purpose. When the condition or boolean value passed to .disabled is true, the button is deactivated.

.disabled(conditionToDisable)

Now let's see this modifier in a real View:

snippet of code with disabled modifier
snippet of code with disabled modifier