Discussion 議論
A value of nil
means that the Button doesn’t have an assigned role. If the button does have a role, use it to make adjustments to the button’s appearance. The following example shows a custom style that uses bold text when the role is cancel
, red
text when the role is destructive
, and adds no special styling otherwise:
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.font(
configuration.role == .cancel ? .title2.bold() : .title2)
.foregroundColor(
configuration.role == .destructive ? Color.red : nil)
}
}
You can create one of each button using this style to see the effect:
VStack(spacing: 20) {
Button("Cancel", role: .cancel) {}
Button("Delete", role: .destructive) {}
Button("Continue") {}
}
.buttonStyle(MyButtonStyle())