I can not seem to get the text in two of my buttons to change to white, unless I manually do it through the storyboard. Any ideas?
class AppUIButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
backgroundColor = Theme.tintColor
layer.cornerRadius = frame.height / 2
setTitleColor(UIColor.white, for: .normal)
}
}
class AddTripViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tripTextField: UITextField!
@IBOutlet weak var cencelButton: UIButton!
@IBOutlet weak var saveButton: UIButton!
var doneSaving: (() -> ())?
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.font = UIFont(name: Theme.mainFontName, size: 24)
//I can force the text color to change with the bellow
//cencelButton.setTitle("Cancel", for: .normal)
//cencelButton.setTitleColor(UIColor.white, for: .normal)
//saveButton.setTitle("Add", for: .normal)
//saveButton.setTitleColor(UIColor.white, for: .normal)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true)
}
@IBAction func save(_ sender: Any) {
TripFunctions.createTrip(tripModel: TripModel(title: tripTextField.text!))
if let doneSaving = doneSaving {
doneSaving()
}
dismiss(animated: true)
}
}
3
Answers
This should work for you.
If you want to create a custom class for UIButton you can do something like this
And you can use it by giving your button class name to the UIButton
This should work for you: