I'm looking for a way to remove the label of a Toggle in SwiftUI...
I have tried with ToggleStyle but it does not seems to be the right way:
Toggle(isOn: $isBlinky) {
Text("DO NOT DISPLAY").color(.red)
}
.toggleStyle(.switch)
As the label seems to be included in the type itself (struct Toggle<Label>) there may be no way to only have the switch alone...
By the way if I use Text("") and then scaledToFit() the switch is still a bit on the right and not really centered...
Anyway if someone has an idea!
PS: While waiting for a solution, I wrapped a good old UISwitch, but that's not the idea...
struct Switch : UIViewRepresentable {
@Binding var isOn : Bool
func makeUIView(context: Context) -> UISwitch {
let uiView = UISwitch()
uiView.addTarget(
context.coordinator,
action: #selector(Coordinator.didChange(sender:)),
for: .valueChanged)
return uiView
}
func updateUIView(_ uiView: UISwitch, context: Context) {
uiView.isOn = isOn
}
// MARK:- Coordinator
func makeCoordinator() -> Switch.Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject {
var control: Switch
init(_ control: Switch) {
self.control = control
}
@objc func didChange(sender: UISwitch) {
control.isOn = sender.isOn
}
}
}
