自定义checkbox in iOS

来源:互联网 发布:金山强力卸载软件 编辑:程序博客网 时间:2024/05/17 23:45

做项目的过程中发现,IOS的SDK中并没有单选框控件。所以想办法将UIButton自定义成checkbox.方法如下


UIImage *uncheckedImg=[UIImage imageNamed:@"check_2x.png"];

UIImage *checkedImg=[UIImage imageNamed:@"checked_2x.png"];

UIButton *checkboxBtn=[UIButton buttonwithType:UIButtonTypeCustom];

[checkboxBtn setBackgroundImage:uncheckedImg forState:UIControlStateNormal];

[checkboxBtn setBackgroundImage:checkedImg forControl:UIControlStateSelected];

[checkboxBtn addTarget:self action:@selector(chooseBox1:) forControlEvents:UIControlEventTouchupInside];


粗体字部分一定要实现,否则不会有按下button打勾的效果

原创粉丝点击