2

I am attempting to program in swift without storyboards/interface builder. When I press a UIButton, it does not change color like it would had I created it on the storyboard file. How can I enable this visual feature?

Here is (roughly) my code:

import UIKit
class MyView: UIView {
    var myButton: UIButton
    init(buttonTitle: String) {
        myButton = UIButton()
        myButton.translateAutoresizingMaskIntoConstraints = false
        myButton.setTitle(buttonTitle, forState: .Normal)
        myButton.setTitleColor(.blueColor(), forState: .Normal)
        super.init(frame: CGRect())
        addSubview(myButton)
        var layoutConstraints = [NSLayoutConstraints]()
        //... autolayout ...
        NSLayoutConstraint.activateConstraints(layoutConstraints)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Thanks in advance.

Mark Anastos
  • 365
  • 3
  • 14

1 Answers1

1

please refer the below link, its from objective c. You can find some concept from here, How to change the background color of a UIButton while it's highlighted?

They have already answered . Got it friend!

Community
  • 1
  • 1
S. Karthik
  • 618
  • 5
  • 16
  • 7
    This is not exactly what I needed, because this simply allows me to change the color of the button when it is pressed, but not to animate the change between these states as the default button does. However, I did find my answer; I just needed to specify in the initialization that it is of UIButtonType.System: `myButton = UIButton(type: .System)`. Thank you. – Mark Anastos Dec 23 '15 at 02:37
  • ohh sorry friend i just want to share it from that you can get some point . Anyhow fine ! cool. thanks – S. Karthik Dec 23 '15 at 02:38