【问题标题】:Set alpha of background of UIButton but not the title设置 UIButton 背景的 alpha 但不设置标题
【发布时间】:2013-07-05 15:57:36
【问题描述】:

当我设置按钮的 alpha 时,它也会影响标题的不透明度。有没有办法只针对背景并将标题 alpha 设置为 1.0?

【问题讨论】:

    标签: ios xcode uibutton


    【解决方案1】:

    这对我有用:

    self.myButton.backgroundColor = [UIColor clearColor];
    

    或者如果您不想让它完全清晰,但仍然具有透明度,您可以使用:

    self.myButton.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.5];
    

    第二个示例会为您提供 alpha 0.5 的灰色。

    SWIFT 4 更新

    myButton.backgroundColor = .clear
    

    myButton.backgroundColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha:0.5)
    

    【讨论】:

    • 我怎么想不到这个?谢谢!
    【解决方案2】:

    您还可以更改情节提要中颜色的 alpha,

    见附图:

    【讨论】:

      【解决方案3】:

      Swift 中:

      import UIKit
      
      class SignInUpMenuTableViewControllerBigButton: UIButton {
      
          required init(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
              self.applyCustomDesign()
          }
      
          func applyCustomDesign() {
              // We set the background color of the UIButton.
              self.clearHighlighted()
          }
      
          override var highlighted: Bool {
              didSet {
                  if highlighted {
                      self.highlightBtn()
                  } else {
                      self.clearHighlighted()
                  }
              }
          }
      
          func highlightBtn() {
              self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
          }
      
          func clearHighlighted() {
              self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.05)
          }
      
      }
      

      斯威夫特 4.2

      import UIKit
      
      class SignInUpMenuTableViewControllerBigButton: UIButton {
      
          required init(coder aDecoder: NSCoder) {
              super.init(coder: aDecoder)
              self.applyCustomDesign()
          }
      
          func applyCustomDesign() {
              // We set the background color of the UIButton.
              self.clearHighlighted()
          }
      
          override var highlighted: Bool {
              didSet {
                  if highlighted {
                      self.highlightBtn()
                  } else {
                      self.clearHighlighted()
                  }
              }
          }
      
          func highlightBtn() {
              self.backgroundColor = UIColor.whiteColor().withAlphaComponent(0.0)
          }
      
          func clearHighlighted() {
              self.backgroundColor = UIColor.whiteColor().withAlphaComponent(0.05)
          }
      
      }
      

      【讨论】:

      • 加一个colorWithAlphaCompnent!对于 Swift 3:UIColor.black.withAlphaComponent(0.8)
      【解决方案4】:

      继承 UIButton 并扩展 setEnabled: 方法似乎可行:

      - (void) setEnabled:(BOOL)enabled {    
          [super setEnabled:enabled];
          if (enabled) {
              self.imageView.alpha = 1;
          } else {
              self.imageView.alpha = .25;
          }
      }
      

      【讨论】:

      • 这对你真的有用吗?在 iOS 7 中,我似乎根本无法更改 Button 图像的 Alpha 设置。
      【解决方案5】:

      使用 UIButton,您可以通过将按钮样式设置为自定义而不是圆形矩形来移除背景。这样可以保持标题不变,但删除了按钮背景。如果您在 Storyboards 中执行此操作,则可以在元素属性中更改按钮样式。对于代码,格式为:

      UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom];
      // Setup frame and add to view
      

      【讨论】:

        【解决方案6】:

        斯威夫特button.backgroundColor = UIColor.white.withAlphaComponent(0.7)

        【讨论】:

          【解决方案7】:

          您只是使用纯色作为背景吗?还是您使用的是图片?

          如果您使用的是图像,那么您可以使用内置 alpha 的图像。

          如果您使用颜色,则可以在颜色上设置 alpha。

          但是,更改任何视图的 alpha 会影响所有子视图。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-05-06
            • 2019-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多