Dalal Alharbi
Dalal Alharbi

Reputation: 191

How to make radio button with swift?

I am trying a lot of code and it's nothing is working for me. I need to make four radio buttons with swift when I show the question for the user to choose one of radio buttons.

Upvotes: 2

Views: 2507

Answers (3)

Marc_Alx
Marc_Alx

Reputation: 1441

The UIControl that offers the closest behavior from radio button in swift iOS is the UISegmentedControl, take a look at the reference.

Here's apple's guidelines for mobile controls.

Some quotes from this article :

Use a segmented control to offer choices that are closely related but mutually exclusive.

A segmented control:

  • Consists of two or more segments whose widths are proportional, based on the total number of segments
  • Can display text or images

Upvotes: -1

Gagan_iOS
Gagan_iOS

Reputation: 4060

You can create your own login by using a mutabledictionary with button tag as key for dictionary. Add green/selected image for selcted button & set "yes" value for key (button tag) in dictionary for selected button & "no" value for other button's tag.

I have an example for the same but in Objective -C

Upvotes: 0

MQLN
MQLN

Reputation: 2328

Sadly, there's not a built in way to do this with Swift, but it's easy enough to code out. I'd personally recommend using this Git project, as I've heard good things about it.

If you'd like to built a radio button from scratch, I'd suggest looking a making a check button by using a regular button with a UIControlStateSelected triggering a check img, and then set the layer.cornerRadius (after setting the clipsToBounds = true of course) of your button to half its width to produce a circle!

Upvotes: 1

Related Questions