Daniel Russo
Daniel Russo

Reputation: 9

Change border color of buttons once pressed

I am trying to have the borders for the buttons change once buttons are pressed, here is how the code currently looks:

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

Upvotes: 0

Views: 114

Answers (3)

Antony Raphel
Antony Raphel

Reputation: 2078

With storyboard, you can do like this:

  • drag and place the button into viewcontroller
  • set the button Tag as per the buttons
  • drag the buttons into .h and create IBOutletCollection NSArray for buttons. Link all buttons into same outlet.
    @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btn;
  • set IBAction common for all the buttons.
    - (IBAction)btnActions:(UIButton *)sender;

common IBAction for uibutton

  • once created IBAction, btnActions: method will be created in .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;
    }
  }
}

Upvotes: 1

Tyun
Tyun

Reputation: 1

will, I guess you maybe need this. You can add two events for the buttons. So set border when the event UIControlEventTouchDown response, and recover it when the event UIControlEventTouchUpInsideresponse.

All this on my Github name 'T_yunButtonBorder' [email protected]:xmy0010/DemoForCSDN.git

Like this:

- (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;
}

Upvotes: 0

Bohm
Bohm

Reputation: 1004

You can add a selector to you button, and use a BOOL property to track the button status and witch the color in the selector. If you have an array of buttons you will need an array of status flags. The code below should work (sorry for any typo. I am not in front of my computer at this time).

@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;
        }
  }

Upvotes: 0

Related Questions