【问题标题】:UIButton background color for highlighted/selected state issue突出显示/选定状态问题的 UIButton 背景颜色
【发布时间】:2016-07-25 07:53:52
【问题描述】:

我有一个UIButton,我创建了一个扩展来为不同的状态添加背景颜色。

我正在使用以下代码:

extension UIButton {

    func setBackgroundColor(color: UIColor, forState: UIControlState) {

        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
        CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
        let colorImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        self.setBackgroundImage(colorImage, forState: forState)
    }
}


// Set Button Title and background color for different states
    self.donateButton.setBackgroundColor(UIColor.redColor(), forState: .Normal)
    self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    self.donateButton.setBackgroundColor(UIColor.greenColor(), forState: .Highlighted)
    self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Highlighted)
    self.donateButton.setBackgroundColor(UIColor.greenColor(), forState: .Selected)
    self.donateButton.setTitleColor(UIColor.whiteColor(), forState: .Selected)

我的问题是它没有为突出显示/选定状态选择正确的 UIButton 背景颜色和标题颜色。

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    我发现了问题,UIButton 设置为 System。我只是将其更改为 Custom,它开始按预期工作。

    【讨论】:

    • 我也遇到了同样的问题,定制就可以了。按下状态是突出显示而不是我想的(选择):S.
    • 谢谢伙计! @Ashish Verma
    【解决方案2】:

    更新 Swift 4

    extension UIButton {
    
        func setBackgroundColor(color: UIColor, forState: UIControlState) {
    
            UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
            UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
            UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let colorImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            self.setBackgroundImage(colorImage, for: forState)
        }
    
    }
    

    事件 按钮动作 高亮显示触摸

    【讨论】:

      【解决方案3】:

      Swift 5、IOS 13+

      extension UIButton {
      
        func setBackgroundColor(_ color: UIColor, for forState: UIControl.State) {
          UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
          UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
          UIGraphicsGetCurrentContext()!.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
          let colorImage = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
          self.setBackgroundImage(colorImage, for: forState)
        }
      
      }
      

      【讨论】:

        【解决方案4】:

        它不起作用的原因是您没有设置正确的状态:您缺少[ .highlighted, .selected ] 状态,因此它回退到普通的.normal

        isSelectedisHighlighted 都为真时,UIKit 会明确查找两者的状态组合,并且不会单独接受它们中的任何一个。

        使用缺失状态扩展的原始代码(并在 Swift 5 中更新为有效语法):

        self.donateButton.setBackgroundColor(.green, for: [ .highlighted, .selected ])
        self.donateButton.setTitleColor(.white, for: [ .highlighted, .selected ])
        

        【讨论】:

          【解决方案5】:

          当您的UIButton 被选中时。如果您专注于按钮,它不会将按钮突出显示为状态.Highlighted。如果未选中,它将根据您的意愿突出显示。因为它认为您的按钮中有背景图像,它将使用默认效果突出显示图像。如果要将状态更改为.Selected,则必须将UIButton的变量selected设置为true。

          【讨论】:

            【解决方案6】:

            要实现自定义按钮照明,您需要: - 在情节提要中设置您想要看到的背景颜色和 alpha,当按下按钮时 - 初始化时,将背景和alpha设置为未按下的按钮 -实现按钮的三种方法(Touch Down / Touch Up Side / Touch Drag Outside) - 在其中,更改按钮照明

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-07-28
              • 2013-01-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多