Shadow_Land
Shadow_Land

Reputation: 57

How to disable interaction while ProgressView showing (SwiftUI)

I have a view with structure like this:

struct ContentView: View {
    @ObservedObject private var viewModel = MyViewModel()
    
    var body: some View {
        ZStack {
            VStack {
                // Several buttons and input forms
            }

            // the viewModel has published states. 
            switch viewModel.state {
                case .loading:
                    ProgressView()
                case .finish:
                    // ...
                case .error:
                    // ...
            }
        }
    }
}

What I want to achieve is to simply avoid user interacting with any other components if the ProgressView appeared, but couldn't find a good way...

Upvotes: 1

Views: 2062

Answers (1)

jnpdx
jnpdx

Reputation: 52377

You can use the .disabled modifier on your controls.

For example:

Button("Button") { //action }.disabled(isLoading)

(You may want some sort convince property like this: private var isLoading : Bool { viewModel.state == .loading } )

In fact, because SwiftUI view modifiers like this will apply down to child views, you can apply it to a whole stack or group of controls:

VStack {
  //controls in here
}.disabled(isLoading)

Upvotes: 3

Related Questions