Skip to content

Instantly share code, notes, and snippets.

@mattyoung
Created April 27, 2020 18:24
Show Gist options
  • Save mattyoung/8efe23c32474386523fcf3da7d9e62bd to your computer and use it in GitHub Desktop.
Save mattyoung/8efe23c32474386523fcf3da7d9e62bd to your computer and use it in GitHub Desktop.
import SwiftUI
struct CheckboxToggleStyle: ToggleStyle {
@Environment (\.isEnabled) var isEnabled
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.label
Image(systemName: configuration.isOn ? "checkmark.square" : "square")
.resizable()
.frame(width: 18, height: 18)
.onTapGesture { configuration.isOn.toggle() }
}
// only .accentColor work, uncomment this and comment out the other one to see
// .foregroundColor(isEnabled ? .accentColor : .gray)
// change color stay .red regardless
.foregroundColor(isEnabled ? .red : .purple)
}
}
struct FilledButton: ButtonStyle {
@Environment (\.isEnabled) var isEnabled
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding()
// .background(isEnabled ? Color.accentColor : Color.green)
.background(isEnabled ? Color.orange : Color.green)
.foregroundColor(.white)
.cornerRadius(8)
}
}
struct CheckboxToggleStyleIsDisabled: View {
@State private var isOn = false
@State private var disabled = false
var body: some View {
VStack {
Spacer()
Text("F'ing color do not change when it's disabled!!")
Toggle(isOn: self.$isOn) {
Text("Turn on")
}
.toggleStyle(CheckboxToggleStyle())
.disabled(self.disabled)
Button("Halleluja Why?") {
}
.buttonStyle(FilledButton())
.disabled(self.disabled)
Toggle(isOn: self.$isOn) {
Text("Turn on")
}
.disabled(self.disabled)
Spacer()
Toggle(isOn: self.$disabled) {
Text("Disable controls above πŸ‘†πŸ‘†πŸ‘†")
}
}
}
}
struct CheckboxToggleStyleIsDisabled_Previews: PreviewProvider {
static var previews: some View {
CheckboxToggleStyleIsDisabled()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment