隐藏的Swiftui进度视图
您好,我想在工具栏按钮项上创建未确定的进度视图。完成后,我想将其隐藏,但hidden()方法没有参数,如disabled(Bool)。当任务完成时,如何隐藏进度视图 这就是我想要的 我不知道如何在swiftui上以编程方式隐藏它,因为它没有参数 这是密码隐藏的Swiftui进度视图,swift,swiftui,uiprogressview,Swift,Swiftui,Uiprogressview,您好,我想在工具栏按钮项上创建未确定的进度视图。完成后,我想将其隐藏,但hidden()方法没有参数,如disabled(Bool)。当任务完成时,如何隐藏进度视图 这就是我想要的 我不知道如何在swiftui上以编程方式隐藏它,因为它没有参数 这是密码 .navigationBarItems(leading: Button(action: { self.presentationMode.wrappedValue.dismiss() }, label: {
.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()
)
可以创建该视图扩展名
extension View {
@ViewBuilder func isHidden(_ isHidden: Bool) -> some View {
if isHidden {
self.hidden()
} else {
self
}
}
}
然后动态隐藏视图:
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
)
}
}
}
struct ContentView:View{
@状态变量isHidden=false
var body:一些观点{
导航视图{
VStack{
文本(“你好,世界”)
按钮(操作:{
self.ishiden.toggle()
})
{
文本(“更改加载”)
}
}
.navigationBarItems(领先:
按钮(操作:{
},标签:{
文本(“取消”)
.foregroundColor(.橙色)
})
,尾随:
ProgressView()
.isHidden(isHidden)/哦,我的朋友有另一个解决方案。如果$isProgressViewShow
状态为false,它将使用EmptyView
.navigationBarItems(leading:
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}, label: {
Text("Cancel")
.foregroundColor(.orange)
})
, trailing: self.isProgressViewShow ?
AnyView(ProgressView()) : AnyView(EmptyView())
)
要隐藏进度视图
self.isProgressViewShow = true
自定义可重用ProgressView-循环
struct CustomProgressView: View {
var title: String
var total: Double = 100
@Binding var isShown: Bool
@Binding var value: Double
var body: some View {
VStack {
ProgressView(value: value, total: total) {
Text(title)
.font(.headline)
.padding()
}
.background(RoundedRectangle(cornerRadius: 25.0)
.fill(Color.white)
.overlay(RoundedRectangle(cornerRadius: 25.0)
.stroke(Color.gray, style: StrokeStyle()))
)
.progressViewStyle(CircularProgressViewStyle(tint: .muckleGreen))
.padding()
}
.padding(.top)
.isHidden(!isShown)
}
}
在您的视图中,您可以这样使用它
VStack {
ZStack {
CustomProgressView(title: "Adding Post", isShown: self.$viewModel.isLoading,
value: self.$viewModel.uploadPercentageComplete)
}
}
你能展示你的代码吗?我实现了它