What is the best way to make a UIButton checkbox?

后端 未结 9 1446
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 04:03

I am trying to make a standard check box for my iPhone app from a UIButton with a title and image. The button image changes between an \"unchecked\" image and

相关标签:
9条回答
  • 2020-12-14 04:38
        UIImage* nonCheckedImage=[UIImage imageNamed:@"ic_check_box_outline_blank_grey600_48dp.png"];//[UIImage init
        UIImage* CheckedImage=[UIImage imageNamed:@"ic_check_box_black_48dp.png"];//[UIImage init
    
        //ic_check_box_black_48dp.png
        [_checkBox setImage:CheckedImage forState:UIControlStateSelected];
        [_checkBox setImage:nonCheckedImage forState:UIControlStateNormal];
    }
    
    - (IBAction)checkBoxToggle:(id)sender {
        _checkBox.selected = !_checkBox.selected; // toggle the selected property, just a simple BOOL
    
    }
    

    the image you can use google icon

    0 讨论(0)
  • 2020-12-14 04:38

    Try this:-

    -(IBAction)clickCheckButton:(UIButton *)sender {
    
        if (sender.tag==0) {
    
        sender.tag = 1;
    
        [sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 
    
        }else
    
        {
    
        sender.tag = 0;
    
        [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal]; } } sender.tag = 0;
    
        [sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
    
        } 
    }
    
    0 讨论(0)
  • 2020-12-14 04:39

    All you need to do is set 2 different images for the states UIControlStateNormal and UIControlStateSelected, then in your selector, changed the selected property of the button.

    Here is a working example (replace image names with your own):

    - (void)loadView {
        // ...
        UIButton *chkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [chkBtn setFrame:CGRectMake(0, 0, 300, 25)];
    
        [chkBtn setImage:[UIImage imageNamed:@"UNCHECKED.png"] 
                forState:UIControlStateNormal];
        [chkBtn setImage:[UIImage imageNamed:@"CHECKED.png"]
                forState:UIControlStateSelected];
    
        [chkBtn addTarget:self 
                   action:@selector(chkBtnHandler:) 
         forControlEvents:UIControlEventTouchUpInside];
    
        // Optional title change for checked/unchecked
        [chkBtn setTitle:@"I am NOT checked!"
                forState:UIControlStateNormal];
        [chkBtn setTitle:@"I am checked!"
                forState:UIControlStateSelected];
    
        [self.view addSubview:chkBtn];
        [chkBtn release], chkBtn = nil;
        // ...
    }
    
    - (void)chkBtnHandler:(UIButton *)sender {
        // If checked, uncheck and visa versa
        [sender setSelected:!sender isSelected];
    }
    
    0 讨论(0)
  • 2020-12-14 04:44

    For anyone interested in the future - instead of doing it yourself just download the link below from GitHub and it has it subclassed from UIControl already and functions perfectly as a checkbox. Also includes a sample project on how easy it is to use:

    https://github.com/Brayden/UICheckbox

    0 讨论(0)
  • 2020-12-14 04:49

    You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.

    UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.

    You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:

    // when you setup your button, set an image for the selected and normal states
    [myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
    [myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
    
    - (void)myCheckboxToggle:(id)sender
    {
        myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
    }
    
    0 讨论(0)
  • 2020-12-14 04:51

    Set the images in the button:

    [button setImage:uncheckedImage forState:UIControlStateNormal]
    [button setImage:checkedImage forState:UIControlStateSelected]
    

    Now all you need to do is:

    button.selected = state
    

    and the correct images will display.

    0 讨论(0)
提交回复
热议问题