UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Custom Toggle Style makes .onTapGesture do nothing

Forums > SwiftUI

So I'm trying to make a customToggleStyle (borrowed Majid Jabrayilov's for this example). I need to run code when the toggle is pressed. It'll check setting to make sure they can do what they are trying to do, and show an alert if they cannot. This looks awesome, but the .onTapGesture does nothing. (Comment out the custom toggle and it works as intended) What is missing? (Edited with another issue). When they tap the toggle, I would like to do a check to make sure they are allowed to make the toggle true (info kept in a Bool)... example was updated and doesn't change the test Bool back to false in the .onTapGesture. (I have the custom toggle commented out to test that piece of it).

struct CheckboxToggleStyle: ToggleStyle {
    func makeBody(configuration: Configuration) -> some View {
        return HStack {
            configuration.label
            Image(systemName: configuration.isOn ? "checkmark.square" : "square")
                .resizable()
                .frame(width: 22, height: 22)
                .onTapGesture { configuration.isOn.toggle() }
        }
    }
}

struct InformationView: View {
    @State private var test = false
    @State private var otherBool = false
    var body: some View {
        VStack {
            Toggle(isOn: self.$test) {
                Text("I like SwiftUI")
            }
                .toggleStyle(CheckboxToggleStyle())
                .onTapGesture {
                print("The toggle was pressed.")
                if otherBool {
                   print("Everything is fine, nothing to see here.")
                } else {
                   print("No, you can't do that, otherBool is false (will be shown with an alert... I can figure that out.)")
                   self.test = false // cannot let them make it true if otherBool is false
                }
            }
        }
    }
}

2      

OK, so it can all be done with a button. But shouldn't it be able to be done with a Toggle?

Button(action: {
  self.test.toggle()
  if self.test && !otherBool {
    print("No, you can't do that.") // Show Alert
    self.test = false
  }
}) {
  Image(systemName: test ? "checkmark.square" : "square")
    .resizable()
    .frame(width: 25, height: 25)
}
.buttonStyle(PlainButtonStyle())

2      

I'm probably missing something here but have you tried moving your otherBool variable into the toggle style and just use it as a flag there to prevent the state from being toggled to on? My first guess would be that the second tap gesture that you add in your view is being ignored and thus doesn't trigger the expected logic.

2      

@ddaehling Of course that is why (last time I try to do coding after taking a pain pill for my back). Thanks.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.