我试图让按钮的边框在按下按钮后发生变化,这是代码当前的样子:
[m_btnEthinic addObject:btnEthnicity1];
[m_btnEthinic addObject:btnEthnicity2];
[m_btnEthinic addObject:btnEthnicity3];
[m_btnEthinic addObject:btnEthnicity4];
[m_btnEthinic addObject:btnEthnicity5];
[m_btnEthinic addObject:btnEthnicity6];
[m_btnEthinic addObject:btnEthnicity7];
[m_btnEthinic addObject:btnEthnicity8];
[m_btnEthinic addObject:btnEthnicity9];
for (UIButton* btn in m_btnEthinic) {
btn.layer.borderWidth = 1;
btn.layer.cornerRadius = 13;
btn.layer.borderColor = COLOR_GRAYBORDERBTN.CGColor;
[btn setBackgroundColor: COLOR_PURP];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
最佳答案
有了 Storyboard,你可以这样做:
- 将按钮拖放到 View Controller 中
- 根据按钮设置按钮标签
- 将按钮拖到
.h
中并为按钮创建 IBOutletCollection NSArray。将所有按钮连接到同一个 socket 。
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btn;
- 为所有按钮设置通用的IBAction。
- (IBAction)btnActions:(UIButton *)sender;
- 一旦创建IBAction,
btnActions:
方法将在.m
中创建
-(IBAction) btnActions: (UIButton * ) sender {
for (UIButton * b in self.btn) {
//checking if already have borders for the buttons
if (b.layer.cornerRadius == 13) {
b.layer.borderWidth = 0;
b.layer.cornerRadius = 0;
}
//setting the borders for the selected button
if (b.tag == sender.tag) {
b.layer.borderWidth = 2;
b.layer.cornerRadius = 13;
b.layer.borderColor = [UIColor yellowColor].CGColor;
}
}
}
关于css - 按下后更改按钮的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41474478/