如何切换是否隐藏按钮的存在?
我们有无条件的 .hidden() 属性;但我需要有条件的版本。
注意:我们确实有 .disabled(bool) 属性可用,但不是 .隐藏( bool ) .
struct ContentView: View {
var body: some View {
ZStack {
Color("SkyBlue")
VStack {
Button("Detect") {
self.imageDetectionVM.detect(self.selectedImage)
}
.padding()
.background(Color.orange)
.foreggroundColor(Color.white)
.cornerRadius(10)
.hidden() // ...I want this to be toggled.
}
}
}
}
最佳答案
对我来说,设置 frame
非常有效。当您不想看到它时,它的高度为零。当你想要计算的大小时,只需将其设置为 nil
:
SomeView
.frame(height: isVisible ? nil : 0)
如果除了隐藏还想禁用它,你可以设置.disabled
使用切换的 bool 值。SomeView
.frame(height: isVisible ? nil : 0)
.disabled(!isVisible)
关于button - 如何有条件地隐藏或禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57420386/