Yes / No RadioButtons in form (swiftUI)

I'm looking to create RadioButton like buttons inside a form that the user can select Yes / No - (true / false).

As of right now, a screen shot and code is below:

So far, I pretty much have the button code and nothing Binded to these two buttons yet.

                    Section("test button                                                        Yes    No") {
                        HStack {
                            Spacer()
                            Button(action:  {
                            }, label: {
                                Image(systemName: "circle")
                                    .renderingMode(.original)
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 18, height: 18)
                            })
                                .padding(10)
                            Button(action: {
                            }, label: {
                                Image(systemName: "circle")
                                    .renderingMode(.original)
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 18, height: 18)
                            })
                        }
                    }

A couple things I'd like to do:

  1. Connect these two buttons to a Bool() - Maybe it should be a group and not separate?
  2. Can only select one at a time. 2a. an OnTapGesture to change button to be a fill when selected.

I do have a couple of custom button styles that are NOT connected to the code yet but thought I'd show these in case additional information is needed. Two different styles, a My ButtonStyle, and a MyButtonPressed style

    struct MyButtonStyle: ButtonStyle {

      func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
          .padding()
          .foregroundColor(.white)
          .background(configuration.isPressed ? Color.red : Color.blue)
          .cornerRadius(8.0)
      }
    }

    struct MyButtonPressed: ButtonStyle {

      func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
          .padding()
          .foregroundColor(.white)
          .background(configuration.isPressed ? Color.red : Color.blue)
          .cornerRadius(8.0)
      }
    }

Thanks!

If you need any additional info, please ask.                         

I would do this.

  • Create a state var to keep state of the radio set
  • As you have just 2 buttons, a Bool is OK
@State var radio1On = true
  • if more than 2, should be an Int value.
  • draw each button by selecting the appropriate systemImage or calling the appropriate drawing code, depending button is on / off: if radio1On, button1 is filled, otherwise empty. Opposite for button2.
  • in the IBAction of each button, just toggle() radio1On
Yes / No RadioButtons in form (swiftUI)
 
 
Q