【问题标题】:Change border color of buttons once pressed按下后更改按钮的边框颜色
【发布时间】:2017-01-04 22:43:07
【问题描述】:

我正在尝试在按下按钮后更改按钮的边框,这是代码当前的外观:

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

【问题讨论】:

  • 这些 btnEthnicity 是通过编程方式还是通过情节提要创建的?
  • 它们是在故事板上制作的
  • 那么你可以试试 IBOutletCollection NSArray with conditions

标签: css objective-c button background border


【解决方案1】:

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

  • 将按钮拖放到视图控制器中
  • 按按钮设置按钮标记
  • 将按钮拖入.h 并为按钮创建IBOutletCollection NSArray。将所有按钮链接到同一个插座。
    @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btn;
  • 为所有按钮设置通用IBAction
    - (IBAction)btnActions:(UIButton *)sender;

  • 一旦创建IBActionbtnActions:方法将在.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;
    }
  }
}

【讨论】:

    【解决方案2】:

    您可以为您的按钮添加一个选择器,并使用一个 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;
            }
      }
    

    【讨论】:

      【解决方案3】:

      会的,我猜你可能需要这个。您可以为按钮添加两个事件。所以在事件UIControlEventTouchDown响应时设置边框,在事件UIControlEventTouchUpInsideresponse时恢复。

      所有这些都在我的 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-01
        • 2019-06-28
        • 2020-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        • 1970-01-01
        相关资源
        最近更新 更多