Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a possibility to deactivate a button in SwiftUI?

Tags:

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.

like image 328
CodierGott Avatar asked Sep 16 '19 20:09

CodierGott


1 Answers

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.

like image 169
matteopuc Avatar answered Oct 04 '22 19:10

matteopuc