Last active
August 25, 2020 14:58
-
-
Save prafullakumar/c6688a6547dea86a8a3e445ff7bc852f to your computer and use it in GitHub Desktop.
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 | |
enum StylesSet { | |
struct CustomToggle: ToggleStyle { | |
func makeBody(configuration: Configuration) -> some View { | |
HStack { | |
configuration.label | |
Spacer() | |
Rectangle() | |
.foregroundColor(configuration.isOn ? .red : .green) | |
.frame(width: 50) | |
.overlay( | |
Text(configuration.isOn ? "Off" : "On").font(.system(size: 10)) | |
.foregroundColor(configuration.isOn ? .red : .green) | |
.frame(width: 25, height: 25) | |
.background(Color.white) | |
.clipShape(Circle()) | |
.offset(x: configuration.isOn ? 10 : -10, y: 0) | |
.animation(Animation.easeInOut(duration: 0.25)) | |
).clipShape(Capsule()) | |
.onTapGesture { configuration.isOn.toggle() } | |
} | |
} | |
} | |
} | |
struct CustomViews: View { | |
@State var active: Bool | |
var body: some View { | |
NavigationView { | |
List { | |
Toggle(isOn: $active, label: { | |
Text("Active") | |
}) | |
.toggleStyle(StylesSet.CustomToggle()) | |
} | |
.navigationTitle("Styling Demo") | |
} | |
} | |
} | |
struct CustomViews_Previews: PreviewProvider { | |
static var previews: some View { | |
CustomViews(active: true) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment