如果SwiftUI中的条件为真,如何在按钮样式之间进行切换? [英] How to change between button styles if a condition is true in SwiftUI?
本文介绍了如果SwiftUI中的条件为真,如何在按钮样式之间进行切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个自定义按钮样式,我想在点击按钮时更改样式。我尝试过这种方式:
Button(action: {
self.pressed.toggle()
})
{
Text("Button")
}.buttonStyle(pressed ? style1() : style2())
但它不工作,它给我一个来自它所属的VStack的错误:
Unable to infer complex closure return type; add explicit type to disambiguate
如果我执行如下操作:
.buttonStyle(style1())
或
.buttonStyle(style2())
然后错误消失,因此不是来自style 1()或style 2()。
推荐答案
它是快速类型检查违规.我建议您改为
Button(action: {
self.pressed.toggle()
})
{
Text("Button")
}.buttonStyle(Your_style(condition: pressed)) // << put conditional styling inside
有关解决方案示例,请参阅Change buttonStyle Modifier based on light or dark mode in SwiftUI
这篇关于如果SwiftUI中的条件为真,如何在按钮样式之间进行切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文