CodierGott
CodierGott

Reputation: 491

Is there a possibility to deactivate a button in SwiftUI?

Is there a possibility to deactivate a button in SwiftUI? Can not find anything?

I want to do a download with Alamofire and then activate the button after successful download.

Upvotes: 7

Views: 7740

Answers (1)

superpuccio
superpuccio

Reputation: 13012

You can use the .disabled modifier. According to the documentation:

Adds a condition that controls whether users can interact with this view.

import SwiftUI

struct ContentView: View {
    @State private var buttonDisabled = true

    var body: some View {
        Button(action: {
            //your action here
        }) {
            Text("CLICK ME!")
        }
        .disabled(buttonDisabled)
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
#endif

You can set your buttonDisabled State var to false when your download completes.

Upvotes: 15

Related Questions