Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Last active August 25, 2020 14:58
Show Gist options
  • Save prafullakumar/c6688a6547dea86a8a3e445ff7bc852f to your computer and use it in GitHub Desktop.
Save prafullakumar/c6688a6547dea86a8a3e445ff7bc852f to your computer and use it in GitHub Desktop.
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