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

标签 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]

最佳答案

有了 Storyboard,你可以这样做:

  • 将按钮拖放到 View Controller 中
  • 根据按钮设置按钮标签
  • 将按钮拖到 .h 中并为按钮创建 IBOutletCollection NSArray。将所有按钮连接到同一个 socket 。
    @property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *btn;
  • 为所有按钮设置通用的IBAction
    - (IBAction)btnActions:(UIButton *)sender;

common IBAction for uibutton

  • 一旦创建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;
    }
  }
}

关于css - 按下后更改按钮的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41474478/

相关文章:

javascript - 根据复选框 oracle apex 禁用或启用按钮

html - 如何将带有一些图标的输入框放在标题栏的右侧

css - 什么可能是 CSS 导致页脚下断的原因

javascript - 允许 MVC 将 Controller 操作响应视为静态内容

iphone - 为什么 performSelector : crash my application?

iphone - 使用 css 多列在 UIWebView 中对 html 进行分页时出错

ios - SwiftUI 按钮比例宽度高度

html - 使用 CSS 将翻转文本添加到 Sprite

ios - "The user is blocked from live streaming."(域=com.google.GTLRErrorObjectDomain代码=403)

javascript - 如何通过将鼠标悬停在同一页面上的图像上来触发 css 动画按钮