ToggleStyles.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import SwiftUI
  2. struct RadioButtonToggleStyle: ToggleStyle {
  3. func makeBody(configuration: Self.Configuration) -> some View {
  4. HStack {
  5. Circle()
  6. .stroke(lineWidth: 2)
  7. .foregroundColor(.secondary)
  8. .frame(width: 20, height: 20)
  9. .overlay {
  10. if configuration.isOn {
  11. Image(systemName: "circle.fill")
  12. }
  13. }
  14. .onTapGesture {
  15. withAnimation {
  16. configuration.isOn.toggle()
  17. }
  18. }
  19. configuration.label
  20. }
  21. }
  22. }
  23. struct CheckboxToggleStyle: ToggleStyle {
  24. var tint = Color.primary
  25. func makeBody(configuration: Configuration) -> some View {
  26. HStack {
  27. RoundedRectangle(cornerRadius: 5)
  28. .stroke(lineWidth: 2)
  29. .foregroundColor(Color.secondary)
  30. .frame(width: 20, height: 20)
  31. .overlay {
  32. if configuration.isOn {
  33. Image(systemName: "checkmark")
  34. .font(.body)
  35. .fontWeight(.bold)
  36. .foregroundColor(tint)
  37. }
  38. }
  39. .onTapGesture {
  40. configuration.isOn.toggle()
  41. }
  42. configuration.label
  43. }
  44. .contentShape(Rectangle()) // make entire HStack tappable
  45. .onTapGesture {
  46. configuration.isOn.toggle()
  47. }
  48. }
  49. }