1

I have the following UIButton:

// create footerView
let footerView: UIView = UIView()
footerView.backgroundColor = UIColor.yellowColor()
self.view.addSubview(footerView)

footerView.snp_makeConstraints { (make) -> Void in
  make.left.equalTo(self.view.snp_left)
  make.right.equalTo(self.view.snp_right)
  make.height.equalTo(self.footerHeight)
  make.bottom.equalTo(self.view.snp_bottom)
}

let footerButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
footerButton.setTitle("OK", forState: UIControlState.Normal)
footerButton.backgroundColor = UIColor.redColor()
footerButton.tintColor = UIColor.whiteColor()
footerButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
footerView.addSubview(footerButton)

footerButton.snp_makeConstraints { (make) -> Void in
  make.center.equalTo(footerView)
  make.left.equalTo(footerView.snp_left).offset(45)
  make.right.equalTo(footerView.snp_right).offset(-45)
}

Edit: I could use

footerButton.setBackgroundImage(redImage, forState: UIControlState.Highlighted)

but this would destroy the auto layout and border radius if I tapp the button.

How can I change the backgroundColor of the button on UIControlState.Highlighted?

  • What is the problem? Does it just not show or do you get an error? Do you change anything else on your UIButton later in the code? – Nils Ziehn Jun 17 '15 at 16:46
  • you cannot set a background color for a specified state only image is possible. – LC 웃 Jun 17 '15 at 16:54
  • Try this. http://stackoverflow.com/questions/26600980/how-do-i-set-uibutton-background-color-forstate-uicontrolstate-highlighted-in-s – Amit89 Jun 17 '15 at 16:56
  • Check this post https://somethingaboutios.wordpress.com/2016/02/09/uibutton-backgroundcolor-for-uicontrolstateselected/ – Gabriel.Massana Feb 09 '16 at 21:05

2 Answers2

1

There is no -setBackgroundColor:forState method. You can create a solid color background image (programatically or included in your resources) though and use

- setBackgroundImage:forState:
Osmund
  • 772
  • 6
  • 13
  • But this would destroy the auto layout and border radius if I tapp the button. –  Jun 17 '15 at 17:05
0

@stephan1001 I have tried this code and it works

class ViewController: UIViewController {

var footerHeight = 70.0

override func viewDidLoad() {
    super.viewDidLoad()
    configUI()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func configUI(){
    // create footerView
    let footerView: UIView = UIView()
    footerView.backgroundColor = UIColor.yellowColor()
    self.view.addSubview(footerView)

    footerView.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(self.view.snp_left)
        make.right.equalTo(self.view.snp_right)
        make.height.equalTo(self.footerHeight)
        make.bottom.equalTo(self.view.snp_bottom)
    }

    let footerButton: MyButton = MyButton.buttonWithType(UIButtonType.Custom) as! MyButton
    footerButton.setTitle("OK", forState: UIControlState.Normal)
    footerButton.backgroundColor = UIColor.redColor()
    footerButton.tintColor = UIColor.whiteColor()
    footerButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
    footerView.addSubview(footerButton)

    footerButton.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(footerView)
        make.left.equalTo(footerView.snp_left).offset(45)
        make.right.equalTo(footerView.snp_right).offset(-45)
    }
}


}

class MyButton: UIButton{
    override var highlighted: Bool {
        willSet(newValue) {
            if ( newValue == true ){
                self.backgroundColor = UIColor.greenColor()
            } else{
                self.backgroundColor = UIColor.redColor()
            }
        }

    }
}
agy
  • 2,804
  • 2
  • 15
  • 22