Skip to content

Instantly share code, notes, and snippets.

@RogueKnight1726
Last active October 2, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RogueKnight1726/e6a3cbde8ed89279307ff6dd01572698 to your computer and use it in GitHub Desktop.
Save RogueKnight1726/e6a3cbde8ed89279307ff6dd01572698 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton()
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
button.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 20).isActive = true
button.setTitle("Click Here", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(toggleAction(sender:)), for: .touchUpInside)
}
@objc func toggleAction(sender: CustomButton){
sender.selectedFlag.toggle()
if sender.selectedFlag{
sender.backgroundColor = .green
} else {
sender.backgroundColor = .clear
}
}
}
class CustomButton: UIButton{
var selectedFlag = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment