【问题标题】:Custom UIButton not highlighting when touched自定义 UIButton 触摸时不突出显示
【发布时间】:2012-12-14 19:03:25
【问题描述】:

我有一个创建自定义 UIButton 的方法,它允许我使用 QuartzCore 更改按钮的颜色。但是按钮在触摸时不会突出显示。

- (UIButton *)makeHeaderButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIFont *textFont = [UIFont boldSystemFontOfSize:12];
    UIColor *textColor = [UIColor colorWithRed:80/255.0 green:109/255.0 blue:145/255.0 alpha:1.0];
    UIColor *backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1.0];
    [button setTitleColor:textColor forState:UIControlStateNormal];
    button.titleLabel.font = textFont;
    button.autoresizesSubviews = YES;
    button.layer.cornerRadius = 8;
    button.layer.borderWidth = 1;
    // next 2 properties set using QuartzCore class, no other way to set button colors
    button.layer.borderColor = [UIColor grayColor].CGColor;
    button.layer.backgroundColor = backgroundColor.CGColor;
    button.clipsToBounds = YES;
    return button;
}

如何使这些按钮像普通的圆形矩形按钮一样突出显示?

【问题讨论】:

    标签: ios xcode uibutton


    【解决方案1】:

    在您的代码中,添加行 button.showsTouchWhenHighlighted = TRUE;

    【讨论】:

    • 这似乎使标题淡出淡入。我希望按钮背景在触摸按钮时闪烁为蓝色。
    • @Bob 你总是可以设置手动突出显示。例如:注册一个事件onTouchDown(或任何它),然后在实现中执行类似button.layer.backgroundColor = [UIColor BlueColor]; 然后onTouchUp 将颜色更改回您的设置
    • 哇,这越来越复杂了。我要做的就是制作一个具有自定义背景颜色的 RoundRect 按钮。当然有更简单的方法来做到这一点。
    • 我最后用我想要的背景颜色制作了一个 png 文件,然后使用 setBackgroundImage:forState: 来更改 roundRect 按钮的颜色。
    • @Bob 很难相信没有办法在高亮状态下设置按钮背景颜色:(
    【解决方案2】:

    在 Storyboard 中设置按钮背景默认图像和突出显示的图像。 在按钮操作上写下这一行,即更改按钮颜色 1 秒

    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    

    【讨论】:

      【解决方案3】:

      有一种简单的方法可以在自定义按钮上实现高亮。 1.为高亮状态创建.png图像(带有颜色,alpha等); 2.进入yourbutton.xib; 3.选择XCode右侧的Attributes Inspector; 4.将状态配置设置为突出显示 5.将您的.png图像设置为背景图像。 不客气:)

      【讨论】:

        【解决方案4】:

        如果有人仍然遇到问题 - 你应该像这样创建 UIButton:

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        btn.layer.cornerRadius = 4;
        btn.layer.masksToBounds = YES;
        
        [btn setTranslatesAutoresizingMaskIntoConstraints:NO];
        
        [btn setBackgroundColor:[UIColor greenColor]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [btn setTitle:@"ok" forState:UIControlStateNormal];
        
        *** any customisation that you would like ***
        
        [parentView addSubview: btn];
        

        因此,这样您仍然可以让 iOS 处理所有突出显示。主要是使用

        [UIButton buttonWithType:UIButtonTypeSystem];
        

        如果您使用 initWithFrame 或任何其他方法 - 您必须实施

        setTitleColor: forState: 
        setBackgroundImage: forState:
        

        【讨论】:

        • 使用系统按钮与自定义按钮是关键。即使按钮是在 Storyboard 或 Nib 上创建的。在 IB 中,在按钮上设置图片会将其更改为自定义按钮,因此您需要手动将其更改回来。
        【解决方案5】:

        您可以创建自己的UIButton 子类(然后您只需在构造函数中设置所有这些属性)。

        而且你必须重写UIButton的highlight方法

        - (void)setHighlighted:(BOOL)highlighted {
            if (highlighted) {
                self.backgroundColor = [UIColor redColor];
            } else {
                self.backgroundColor = [UIColor blueColor];
            }
        }
        

        【讨论】:

          【解决方案6】:

          对于Swift,创建一个继承自UIButton 的自定义类并覆盖isHighlighted 属性:

          class ButtonWithHighlight: UIButton {
          
              override var isHighlighted: Bool {
                  get {
                      return super.isHighlighted
                  }
                  set {
                      if newValue {
                          backgroundColor = <#color when highlighted#>
                      }
                      else {
                          backgroundColor = <#color for normal state#>
                      }
                      super.isHighlighted = newValue
                  }
              }
          
          }
          

          【讨论】:

          • 处理 alpha 而不是 backgroundColor 似乎效果更好
          • @AntonTropashko 如果突出显示颜色与正常状态颜色相同可能会更好,但对于您想要白色正常按钮和绿色突出显示的情况,它不起作用
          【解决方案7】:

          在 Swift 中,您还可以覆盖 isHighlighted 变量并在其上添加 alpha 动画。

          override var isHighlighted: Bool {
              didSet {
                  UIView.animate(withDuration: 0.25, delay: 0, options: [.beginFromCurrentState, .allowUserInteraction], animations: {
                      self.alpha = self.isHighlighted ? 0.5 : 1
                  }, completion: nil)
              }
          }
          

          【讨论】:

          • 当您点击并按住 isHighlighted 时,会生成大量重复事件。因此,最好先检查值是否发生变化:guard oldValue != self.isHighlighted else { return }
          猜你喜欢
          • 2014-02-03
          • 2014-09-10
          • 1970-01-01
          • 1970-01-01
          • 2011-05-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-18
          • 2015-08-28
          相关资源
          最近更新 更多