按下后更改按钮的边框颜色

时间:2017-01-04 22:43:07

标签: css objective-c button background border

按钮按下后,我试图让按钮的边框发生变化,以下是代码当前的外观:

[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]

3 个答案:

答案 0 :(得分:1)

使用故事板,您可以这样做:

  • 将按钮拖放到viewcontroller
  • 根据按钮
  • 设置按钮标记
  • 将按钮拖动到 foreach my $line1 (@file1) 并为按钮创建 IBOutletCollection NSArray。将所有按钮链接到同一插座 .h
  • 为所有按钮设置 IBAction @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btn;

common IBAction for uibutton

  • 创建 IBAction 后,- (IBAction)btnActions:(UIButton *)sender;
  • 将创建btnActions:方法
.m

答案 1 :(得分:0)

您可以为您的按钮添加一个选择器,并使用BOOL属性来跟踪按钮状态并选择选择器中的颜色。如果您有一组按钮,则需要一组状态标志。下面的代码应该可以工作(抱歉任何错字。此时我不在我的电脑前。)

@property (strong, nonatomic) borderStatus;

-(void)viewDidLoad {
    borderStatus = NO;
    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

 -(void)buttonPressed:(id)sender {
        if(borderStatus){
              ((UIButton*)sender).layer.borderColor = [UIColor redColor];
              borderStatus = NO;
        } else {
              ((UIButton*)sender).layer.borderColor = [UIColor greenColor];
              borderStatus = YES;
        }
  }

答案 2 :(得分:0)

会,我想你可能需要这个。您可以为按钮添加两个事件。因此,在事件UIControlEventTouchDown响应时设置边框,并在事件UIControlEventTouchUpInside响应时恢复。

所有这一切都在我的Github名称'T_yunButtonBorder'git @ github.com:xmy0010 / DemoForCSDN.git

像这样:

- (void)viewDidLoad {
    [super viewDidLoad];

     NSMutableArray *m_btnEthinic = @[].mutableCopy;
     for (int i = 0; i < 8; i++) {

         UIButton *btnEthnicity = [UIButton buttonWithType:UIButtonTypeCustom];
         [btnEthnicity setTitle:[NSString stringWithFormat:@"btn%d", i] forState:UIControlStateNormal];
         btnEthnicity.frame = CGRectMake(50, 50 + 40 * i, 50, 20);
         btnEthnicity.backgroundColor = [UIColor redColor];
         [btnEthnicity addTarget:self action:@selector(btnEthnicityTouchDown:) forControlEvents: UIControlEventTouchDown];
         [btnEthnicity addTarget:self action:@selector(btnEthnicityTouchUp:) forControlEvents: UIControlEventTouchUpInside];
         [self.view addSubview:btnEthnicity];
         [m_btnEthinic addObject:btnEthnicity];
     }
}


- (void)btnEthnicityTouchDown:(UIButton *)sender {

    NSLog(@"down");
    sender.layer.borderWidth = 2;
    sender.layer.cornerRadius = 10;
    sender.layer.borderColor = [UIColor greenColor].CGColor;
}

- (void)btnEthnicityTouchUp:(UIButton *)sender {

    NSLog(@"up");
    sender.layer.borderWidth = 0;
    sender.layer.cornerRadius = 0;
}