Skip to content

Instantly share code, notes, and snippets.

@jboullianne
Created August 18, 2020 02:19
Show Gist options
  • Save jboullianne/12c089f5943f9850632b9d0cfbd72e76 to your computer and use it in GitHub Desktop.
Save jboullianne/12c089f5943f9850632b9d0cfbd72e76 to your computer and use it in GitHub Desktop.
Custom SwiftUI ToggleStyle - Checkmark
import SwiftUI
struct CheckmarkToggleStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.label
Spacer()
Rectangle()
.foregroundColor(configuration.isOn ? .green : .gray)
.frame(width: 51, height: 31, alignment: .center)
.overlay(
Circle()
.foregroundColor(.white)
.padding(.all, 3)
.overlay(
Image(systemName: configuration.isOn ? "checkmark" : "xmark")
.resizable()
.aspectRatio(contentMode: .fit)
.font(Font.title.weight(.black))
.frame(width: 8, height: 8, alignment: .center)
.foregroundColor(configuration.isOn ? .green : .gray)
)
.offset(x: configuration.isOn ? 11 : -11, y: 0)
.animation(Animation.linear(duration: 0.1))
).cornerRadius(20)
.onTapGesture { configuration.isOn.toggle() }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment