1

我有一个包含 5 个元素和一个取消按钮的操作表。我正在尝试将取消按钮设置为红色,但它不起作用。我已经看到这个SwiftUI ActionSheet 每个动作都有不同的颜色。破坏性按钮样式使其他选项卡变为红色,但不知道如何将取消选项卡变为红色。我已将我的取消选项卡更新为以下,但颜色仍然是蓝色,任何建议都会很棒。

.cancel(Text("Cancel")
.font(.system(size: 40.0))
.foregroundColor(Color.red))

下面是我的 ActionSheet 代码:

.actionSheet(isPresented: $showLocationOptions) {
    ActionSheet(title: Text("Which city/town is this place in ?"), message: Text("Select a location"), buttons: [

    .default(Text(location1)) {  },
    .default(Text(location2)) {  },
    .default(Text(location3)) {  },
    .default(Text(location4)) {  },
    .default(Text(location5)) {  },
    .cancel(Text("Cancel")
    .font(.system(size: 40.0))
    .foregroundColor(Color.red))
    
    ])
}
4

1 回答 1

1

没有明确的方法,但作为解决方法,您可以使用破坏性样式来显式命名取消按钮,并使用 nop 操作,例如

.actionSheet(isPresented: $showLocationOptions) {
    ActionSheet(title: Text("Which city/town is this place in ?"), message: Text("Select a location"), buttons: [

    .default(Text(location1)) {  },
    .default(Text(location2)) {  },
    .default(Text(location3)) {  },
    .default(Text(location4)) {  },
    .default(Text(location5)) {  },
    .destructive(Text("Cancel")){       // << keep as last
            // just nop - will be just closed
        }    
    ])
}
于 2021-03-26T08:17:49.477 回答