如何使 Button 有条件地隐藏或禁用?
How do you make a Button conditionally hidden or disabled?
如何切换是否隐藏按钮?
我们有非条件 .hidden() 属性;但我需要 conditional 版本。
注意:我们确实有 .disabled(bool) 属性 可用,但没有 .hidden(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.
}
}
}
}
我希望 hidden
修饰符稍后得到参数,但从那时起,改为设置 alpha:
@State var shouldHide = false
var body: some View {
Button("Button") { self.shouldHide = true }
.opacity(shouldHide ? 0 : 1)
}
它并不总是一个很好的解决方案,但在某些情况下,有条件地添加它也可能有效:
if shouldShowMyButton {
Button(action: {
self.imageDetectionVM.detect(self.selectedImage)
}) {
Text("Button")
}
}
在不显示的情况下会出现空space的问题,根据具体排版的不同或多或少会有问题。这可以通过添加一个 else
语句来解决,该语句或者添加一个等效大小的空白 space.
您可以利用 SwiftUI 的新双向绑定并添加一个 if 语句:
struct ContentView: View {
@State var shouldHide = false
var body: some View {
ZStack {
Color("SkyBlue")
VStack {
if !self.$shouldHide.wrappedValue {
Button("Detect") {
self.imageDetectionVM.detect(self.selectedImage)
}
.padding()
.background(Color.orange)
.foregroundColor(Color.white)
.cornerRadius(10)
}
}
}
}
}
与将不透明度设置为 0 相比,这样做的好处是它会从你的 UI 中删除怪异的 spacing/padding,因为按钮仍在视图中,只是不可见(如果该按钮位于其他视图组件之间,即)。
对我来说,当您不想看到它时,将 frame
的高度设置为零非常有效。当你想要计算大小时,只需将其设置为 nil
:
SomeView
.frame(height: isVisible ? nil : 0)
如果除了隐藏它之外还想禁用它,可以使用切换布尔值设置 .disabled
。
SomeView
.frame(height: isVisible ? nil : 0)
.disabled(!isVisible)
这里的所有答案都专门针对有条件隐藏的按钮。
我认为可能有帮助的是有条件地制作修饰符本身,例如:
.hidden 用于 button/view,或者 .italic 用于文本等..
使用扩展。
对于有条件的斜体文本很容易,因为 .italic 修饰符 returns Text:
extension Text {
func italicConditionally(isItalic: Bool) -> Text {
isItalic ? self.italic() : self
}
}
然后像这样应用条件斜体:
@State private var toggle = false
Text("My Text")
.italicConditionally(isItalic: toggle)
但是对于 Button 来说这很棘手,因为 .hidden 修饰符 returns "some view":
extension View {
func hiddenConditionally(isHidden: Bool) -> some View {
isHidden ? AnyView(self.hidden()) : AnyView(self)
}
}
然后像这样应用条件隐藏:
@State private var toggle = false
Button("myButton", action: myAction)
.hiddenConditionally(isHidden: toggle)
您可以使用条件语句在 SwiftUI 中轻松隐藏视图。
struct TestView: View{
@State private var isVisible = false
var body: some View{
if !isVisible {
HStack{
Button(action: {
isVisible.toggle()
// after click you'r view will be hidden
}){
Text("any view")
}
}
}
}
}
如何切换是否隐藏按钮?
我们有非条件 .hidden() 属性;但我需要 conditional 版本。
注意:我们确实有 .disabled(bool) 属性 可用,但没有 .hidden(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.
}
}
}
}
我希望 hidden
修饰符稍后得到参数,但从那时起,改为设置 alpha:
@State var shouldHide = false
var body: some View {
Button("Button") { self.shouldHide = true }
.opacity(shouldHide ? 0 : 1)
}
它并不总是一个很好的解决方案,但在某些情况下,有条件地添加它也可能有效:
if shouldShowMyButton {
Button(action: {
self.imageDetectionVM.detect(self.selectedImage)
}) {
Text("Button")
}
}
在不显示的情况下会出现空space的问题,根据具体排版的不同或多或少会有问题。这可以通过添加一个 else
语句来解决,该语句或者添加一个等效大小的空白 space.
您可以利用 SwiftUI 的新双向绑定并添加一个 if 语句:
struct ContentView: View {
@State var shouldHide = false
var body: some View {
ZStack {
Color("SkyBlue")
VStack {
if !self.$shouldHide.wrappedValue {
Button("Detect") {
self.imageDetectionVM.detect(self.selectedImage)
}
.padding()
.background(Color.orange)
.foregroundColor(Color.white)
.cornerRadius(10)
}
}
}
}
}
与将不透明度设置为 0 相比,这样做的好处是它会从你的 UI 中删除怪异的 spacing/padding,因为按钮仍在视图中,只是不可见(如果该按钮位于其他视图组件之间,即)。
对我来说,当您不想看到它时,将 frame
的高度设置为零非常有效。当你想要计算大小时,只需将其设置为 nil
:
SomeView
.frame(height: isVisible ? nil : 0)
如果除了隐藏它之外还想禁用它,可以使用切换布尔值设置 .disabled
。
SomeView
.frame(height: isVisible ? nil : 0)
.disabled(!isVisible)
这里的所有答案都专门针对有条件隐藏的按钮。
我认为可能有帮助的是有条件地制作修饰符本身,例如: .hidden 用于 button/view,或者 .italic 用于文本等..
使用扩展。
对于有条件的斜体文本很容易,因为 .italic 修饰符 returns Text:
extension Text {
func italicConditionally(isItalic: Bool) -> Text {
isItalic ? self.italic() : self
}
}
然后像这样应用条件斜体:
@State private var toggle = false
Text("My Text")
.italicConditionally(isItalic: toggle)
但是对于 Button 来说这很棘手,因为 .hidden 修饰符 returns "some view":
extension View {
func hiddenConditionally(isHidden: Bool) -> some View {
isHidden ? AnyView(self.hidden()) : AnyView(self)
}
}
然后像这样应用条件隐藏:
@State private var toggle = false
Button("myButton", action: myAction)
.hiddenConditionally(isHidden: toggle)
您可以使用条件语句在 SwiftUI 中轻松隐藏视图。
struct TestView: View{
@State private var isVisible = false
var body: some View{
if !isVisible {
HStack{
Button(action: {
isVisible.toggle()
// after click you'r view will be hidden
}){
Text("any view")
}
}
}
}
}