Reputation: 2274
I am having a problem when adding a UIButton
programatically. There is no "tap-animation".
Does anyone know why that occurs and how to fix this? I couldn't find anything on this topic...
let weiterButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.setTitle("WEITER", for: .normal)
v.titleLabel?.font = UIFont(name: "AvenirNext-Bold", size: 20)
v.titleLabel?.textColor = .white
v.backgroundColor = UIColor(red: 75/255, green: 75/255, blue: 75/255, alpha: 1)
v.addTarget(self, action: #selector(weiterButtonTapped), for: .touchUpInside)
return v
}()
Animation I would like to have:
Upvotes: 2
Views: 239
Reputation: 656
You have to set lazy var
instead of let
because you want to get out of your variable(button). Also, you have to set UIButton(type: .system)
because you tell that button should have some kind of style.
lazy var weiterButton: UIButton = {
let v = UIButton(type: .system)
v.translatesAutoresizingMaskIntoConstraints = false
v.setTitle("WEITER", for: .normal)
v.titleLabel?.font = UIFont(name: "AvenirNext-Bold", size: 20)
v.setTitleColor(.white, for: .normal)
// If you want a different color when it is pressed
v.setBackgroundImage(UIImage(named: "Hlighlighted image"), for: .highlighted)
v.setBackgroundImage(UIImage(named: "Your image"), for: .normal)
v.addTarget(self, action: #selector(weiterButtonTapped), for: .touchUpInside)
return v
}()
Upvotes: 2