如何在状态中设置UIButton背景颜色:UIControlState.Highlighted在Swift中

Dav*_*ood 41 uibutton ios swift xcode6

我可以设置按钮的背景颜色,但我无法弄清楚如何设置背景颜色 UIControlState.Highlighted.它甚至可能吗?还是我需要沿着这setBackgroundImage条路走?

win*_*zed 116

如果有人停下来,另一种方式可能更容易,如果它是你需要不止一次...我为UIButton写了一个简短的扩展,它的工作正常:

对于Swift 3

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

对于Swift 4

extension UIButton {
    func setBackgroundColor(color: UIColor, forState: UIControl.State) {
        self.clipsToBounds = true  // add this to maintain corner radius
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
            let colorImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            self.setBackgroundImage(colorImage, for: forState)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你使用它就像setBackgroundImage:

yourButton.setBackgroundColor(color: UIColor.white, forState: UIControl.State.highlighted)
Run Code Online (Sandbox Code Playgroud)

  • 这将破坏自动布局并移除角半径. (13认同)
  • 为了修复角半径,您只需要在按钮上启用剪辑子视图 (12认同)
  • 对我来说,使用颜色部分的初始化程序扩展UIImage是完全值得的.然后这个函数的主体变成简单的`self.setBackgroundImage(UIImage(color:color),forState:forState)` (2认同)

Mav*_*ick 32

语法更改为Swift 3+语法的@winterized扩展名

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)
    }}
Run Code Online (Sandbox Code Playgroud)


Ste*_*erg 16

以下是一种方法.两个IBActions.一个按下按钮时控制背景颜色,一个在释放时控制.

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var button: UIButton!

    @IBAction func buttonClicked(sender: AnyObject) { //Touch Up Inside action
        button.backgroundColor = UIColor.whiteColor()
    }

    @IBAction func buttonReleased(sender: AnyObject) { //Touch Down action
        button.backgroundColor = UIColor.blueColor()

    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}
Run Code Online (Sandbox Code Playgroud)

在添加句点后查看按钮的自动完成选项时,可以设置背景颜色,但不能设置指定的状态.您只能设置背景图像.当然,如果你结婚这样做而不是使用我上面显示的方法,你可以使用setbackgroundImageForState属性加载所需颜色的图像作为背景图像.

在此输入图像描述

在此输入图像描述

  • 该解决方案仅适用于 ViewController 而不适用于 TableViewController。触地动作也适用于 TVC,但只有几毫秒的延迟,因此快速触摸屏幕不会改变按钮的颜色。我认为按钮所在的单元格有问题,但我不确定。 (2认同)

Max*_*elc 7

已接受答案的 Swift 4+ 兼容性:

extension UIButton {

  /// Sets the background color to use for the specified button state.
  func setBackgroundColor(color: UIColor, forState: UIControlState) {

    let minimumSize: CGSize = CGSize(width: 1.0, height: 1.0)

    UIGraphicsBeginImageContext(minimumSize)

    if let context = UIGraphicsGetCurrentContext() {
      context.setFillColor(color.cgColor)
      context.fill(CGRect(origin: .zero, size: minimumSize))
    }

    let colorImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    self.clipsToBounds = true
    self.setBackgroundImage(colorImage, for: forState)
  }
}
Run Code Online (Sandbox Code Playgroud)

兼容 SwiftLint 并修复自动布局/圆角半径损坏的错误。


小智 5

此解决方案的 Swift 4版本:

extension UIButton {

    func setBackgroundColor(_ color: UIColor, for state: 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()

        setBackgroundImage(colorImage, for: state)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 为了避免强行包装,对代码进行了小幅改进,请尝试执行以下操作:`if let currentGraphicsContext = UIGraphicsGetCurrentContext(){currentGraphicsContext.setFillColor(color.cgColor)currentGraphicsContext.fill(CGRect(x:0,y:0,width: 1,高度:1))}`另外添加`layer.masksToBounds = true`以保留`layer.cornerRadius`的功能 (4认同)