Skip to content

Instantly share code, notes, and snippets.

@jboullianne
Created August 18, 2020 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jboullianne/3ab639fbe7ed32ed7df08cfc4b74a164 to your computer and use it in GitHub Desktop.
Save jboullianne/3ab639fbe7ed32ed7df08cfc4b74a164 to your computer and use it in GitHub Desktop.
Custom SwiftUI ToggleStyle - Image Toggle
import SwiftUI
struct ImageToggleStyle: ToggleStyle {
var onImageName: String
var offImageName: String
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.label
Spacer()
Image(configuration.isOn ? onImageName : offImageName)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 51, height: 31, alignment: .center)
.overlay(
Circle()
.foregroundColor(.white)
.padding(.all, 3)
.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