61

Possible Duplicate:
Checkbox in IPhone application

I want to create a simple checkbox with 2 values and save this, how can I make that?

Thanks.

Community
  • 1
  • 1
Doom
  • 1,258
  • 3
  • 15
  • 24
  • 1
    please search before you post. – Mat Mar 20 '11 at 11:36
  • Warning: the native component for a toggle widget is the UISwitch. The accepted answer implements a windows style checkbox with an UIButton. – Jano Jun 17 '16 at 10:42
  • You can simply set the appropriate images from SF symbol for normal and selected state. Refer https://stackoverflow.com/a/69392377/1311902 for detailed answer – Kaunteya Sep 30 '21 at 12:40

2 Answers2

122

Yeah, no checkbox for you in iOS (-:

Here, this is what I did to create a checkbox:

UIButton *checkbox;
BOOL checkBoxSelected;
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(x,y,20,20)];
// 20x20 is the size of the checkbox that you want
// create 2 images sizes 20x20 , one empty square and
// another of the same square with the checkmark in it
// Create 2 UIImages with these new images, then:

[checkbox setBackgroundImage:[UIImage imageNamed:@"notselectedcheckbox.png"]
                    forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@"selectedcheckbox.png"]
                    forState:UIControlStateSelected];
[checkbox setBackgroundImage:[UIImage imageNamed:@"selectedcheckbox.png"]
                    forState:UIControlStateHighlighted];
checkbox.adjustsImageWhenHighlighted=YES;
[checkbox addTarget:(nullable id) action:(nonnull SEL) forControlEvents:(UIControlEvents)];
[self.view addSubview:checkbox];

Now in the target method do the following:

-(void)checkboxSelected:(id)sender
{
    checkBoxSelected = !checkBoxSelected; /* Toggle */
    [checkbox setSelected:checkBoxSelected];
}

That's it!

Gal Blank
  • 2,070
  • 2
  • 18
  • 18
  • 3
    Just a very very very small comment. Don't forget the @ before the strings: @"notselectedcheckbox.png". – polyclick Oct 20 '11 at 11:23
  • 9
    and also if you have multiple buttons you can use this on your taget method `UIButton *btn = (UIButton *) sender; if([btn isSelected]){ [btn setSelected:NO]; }else{ [btn setSelected:YES]; }` – drexsien Oct 26 '11 at 01:52
  • 1
    the two lines where you define the highlighted state aren't needed! they just glitch the checkbox (atleast in iOS5). :) – Martin Herman Apr 27 '12 at 21:39
  • You can do without graphics if you use this approach instead: http://stackoverflow.com/a/2615036/1431728. – JohnK Jun 02 '13 at 02:39
  • 1
    I think using setImage is better than setBackgroundImage, because the image will not stretched if we increased the size of button to increase the selection area – Hossam Ghareeb Feb 11 '14 at 16:30
  • Added `button states` using interface builder. Worked fine. Thanks. – Paritosh Aug 25 '15 at 15:43
  • You can also just use switches. I didnt think of that at first. -.- – Xitcod13 Nov 16 '16 at 22:53
37

On iOS there is the switch UI component instead of a checkbox, look into the UISwitch class. The property on (boolean) can be used to determine the state of the slider and about the saving of its state: That depends on how you save your other stuff already, its just saving a boolean value.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • 1
    I prefer the UISwitch as well, as it's native, optimised and easily customisable. The first answer is also great, but for me (and that's only personal pref) there's no need to re-invent the wheels if not needed. – Septronic Feb 27 '16 at 01:14
  • switch is of no use if we want to allow users to select multiple choices. – Hiren Prajapati Jun 30 '17 at 11:25
  • 2
    @Hiren Prajapati: Why? In case you need to get multiple boolean choices you'd use multiple UISwitch elements, as it is done in the main Preference application. The only culprit with using a UISwitch instead of a checkmark is just aesthetically/representative for those cases when the UI should convey a "Done/Not Done" state for something. – valeCocoa Aug 08 '17 at 23:01
  • 1
    Unfortunately when you do have a list of items and you want a user to select some of them, a switch is much less aesthetically pleasing – Quinn Oct 17 '19 at 14:09
  • In iOS 14.0, there is now a "UISwitchStyle" property with a "UISwitchStyleCheckbox" appearance for UISwitch, showing a traditional check box. – fishinear Mar 04 '21 at 17:59
  • @fishinear The [docs](https://developer.apple.com/documentation/uikit/uiswitch/style/checkbox) say "UISwitch.Style.checkbox is only available to Mac apps built with Mac Catalyst that use the UIUserInterfaceIdiom.mac idiom." – Reinhard Männer Mar 29 '21 at 10:32
  • @ReinhardMänner Darn, you are correct. Weird that they state "Availability iOS 14.0+" when it does not work on iOS 14, though. You can use the style, but it does not show up as a checkbox... – fishinear Mar 30 '21 at 11:46