简体   繁体   中英

Swift - UIButton not highlighted while pressed

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:

Animation

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
}()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM