button - 如果 SwiftUI 中的条件为真,如何在按钮样式之间进行更改?
问题描述
我有 2 种自定义按钮样式,我想在点击按钮时更改样式。我试过这样:
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())
然后错误消失了,所以它不是来自 style1() 或 style2()。
解决方案
这是快速的类型检查违规......我建议改为
Button(action: {
self.pressed.toggle()
})
{
Text("Button")
}.buttonStyle(Your_style(condition: pressed)) // << put conditional styling inside
请参阅基于 SwiftUI 中的亮或暗模式更改按钮样式修饰符中的示例解决方案
推荐阅读
- nix - 如何使用 nix build 引用命令行参数?
- angular - 角材料 - 垫表动态页脚/标题 rowDef
- prisma - 带条件的 Prisma 查询
- mysql - 根据 AMDP/MYSQL 中的连续日期汇总行的列值
- php - 使查询不相等
- php - 购买新订阅时取消之前的 WooCommerce 订阅
- angular - Angular10:“可观察”类型上不存在属性“地图”
' - flutter - Flutter Firebase 身份验证错误
- amazon-web-services - boto3:Table.get_item 不返回项目
- python - discord.py 命令没有响应