Home > Enterprise >  SwiftUI ProgressView not showing when inside List
SwiftUI ProgressView not showing when inside List

Time:03-10

In the following simple example you will find that the first time you tap Toggle Loading the ProgressView is shown as it should, but the second time (3rd tap) it's not. It seems to be caused by the surrounding List.

Any ideas what the issue is and how to make it work?

struct ContentView: View {

    @State private var isLoading = false

    var body: some View {
        List {
            if isLoading {
                HStack(alignment: .center, spacing: 10) {
                    ProgressView()
                    Text("Loading")
                }
            } else {
                Text("Not Loading")
            }

            Button("Toggle Loading") {
                isLoading.toggle()
            }
        }
    }
}

CodePudding user response:

struct ContentView: View {
  
  @State private var isLoading = false
  
  var body: some View {
    List {
      HStack(alignment: .center, spacing: 10) {
        if isLoading {
          ProgressView()
        }
        Text(isLoading ? "Loading" : "Not Loading")
      }
      
      Button("Toggle Loading") {
        isLoading.toggle()
      }
    }
  }
}
  • Related