Skip to content

Instantly share code, notes, and snippets.

@rajohns08
Last active September 29, 2021 14:59
Show Gist options
  • Save rajohns08/cee05b2521888e628657 to your computer and use it in GitHub Desktop.
Save rajohns08/cee05b2521888e628657 to your computer and use it in GitHub Desktop.
iOS / Swift - UIButton subclass to highlight when tapped
import UIKit
class HighlightButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setTitleColor(<#button text color when tapped#>, forState: .Highlighted)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override var highlighted: Bool {
get {
return super.highlighted
}
set {
if newValue {
backgroundColor = <#color when tapped#>
}
else {
backgroundColor = <#color for normal state#>
}
super.highlighted = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment