swiftswiftuiuiprogressview

Swiftui Progress View Hidden


Hello I want to make undetermined Progress View on bar button item. when its done I want to make it hidden, but the hidden() method doesn't have parameter like disabled(Bool). how can I hide the progress view when the task getting done?

This is what I want enter image description here

I don't know how to hide it programmatically on swiftui because it has no parameter.

this is the code

.navigationBarItems(leading:
   Button(action: {
      self.presentationMode.wrappedValue.dismiss()
   }, label: {
        Text("Cancel")
            .foregroundColor(.orange)
    })
 , trailing:
    //this should be hidden when the work done not always
    ProgressView()
        .hidden()
  )

Solution

  • You can create that ViewExtension

    extension View {
        @ViewBuilder func isHidden(_ isHidden: Bool) -> some View {
            if isHidden {
                self.hidden()
            } else {
                self
            }
        }
    }
    

    And then dynamically hide the view:

    struct ContentView : View {
        
        @State var isHidden = false
        
        var body : some View {
            
            NavigationView {
                VStack {
                    Text("Hello World")
                
                    Button(action: {
                        self.isHidden.toggle()
                    })
                    {
                        Text("Change loading")
                    }
                }
                .navigationBarItems(leading:
                   Button(action: {
                   }, label: {
                        Text("Cancel")
                            .foregroundColor(.orange)
                    })
                 , trailing:
                    ProgressView()
                    .isHidden(isHidden) //<< isHidden takes a bool whether it should be hidden
                )
            }
        }
    }