Created
August 18, 2020 02:19
-
-
Save jboullianne/12c089f5943f9850632b9d0cfbd72e76 to your computer and use it in GitHub Desktop.
Custom SwiftUI ToggleStyle - Checkmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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