Toggle("DefaultToggleStyle", isOn: $toggleState)
.toggleStyle(DefaultToggleStyle())
Toggle("SwitchToggleStyle", isOn: $toggleState)
.toggleStyle(SwitchToggleStyle())
struct PurpleTextToggleStyle: ToggleStyle {
var label = ""
func makeBody(configuration: Self.Configuration) -> some View {
Toggle(isOn: configuration.$isOn) {
Text(label)
.foregroundColor(.purple)
.fontWeight(.light)
.font(.title)
}.padding(.horizontal)
}
}
struct CheckMarkToggleStyle: ToggleStyle {
var label = ""
var color = Color.primary
func makeBody(configuration: Self.Configuration) -> some View {
HStack {
Text(label)
Spacer()
Button(action: { configuration.isOn.toggle() } )
{
Image(systemName: configuration.isOn
? "checkmark.square.fill"
: "square.fill")
.foregroundColor(color)
}
}
.font(.title)
.padding(.horizontal)
}
}
struct ColoredToggleStyle: ToggleStyle {
var onColor = Color(UIColor.green)
var offColor = Color(UIColor.systemGray5)
var thumbColor = Color.white
func makeBody(configuration: Self.Configuration) -> some View {
HStack {
configuration.label // The text (or view) portion of the Toggle
Spacer()
RoundedRectangle(cornerRadius: 16, style: .circular)
.fill(configuration.isOn ? onColor : offColor)
.frame(width: 50, height: 29)
.overlay(
Circle()
.fill(thumbColor)
.shadow(radius: 1, x: 0, y: 1)
.padding(1.5)
.offset(x: configuration.isOn ? 10 : -10))
.animation(Animation.easeInOut(duration: 0.2))
.onTapGesture { configuration.isOn.toggle() }
}
.font(.title)
.padding(.horizontal)
}
}