Is there something different when you add a UIButton on top of a UITableView? Or is my code for adding the UIButton incorrect that's causing the button to not look like it gets selected
let addStudentButton = UIButton()
addStudentButton.translatesAutoresizingMaskIntoConstraints = false
let views = ["addStudentButton": addStudentButton]
var allConstraints = [NSLayoutConstraint]()
addStudentButton.setTitle("hi", for: .normal)
// addStudentButton.addTarget(self, action: #selector(StudentsViewController.addStudent), for: UIControlEvents.touchUpInside)
addStudentButton.titleLabel?.font = UIFont.systemFont(ofSize: 20.0)
addStudentButton.setTitleColor(UIColor.flatWhite(), for: .normal)
addStudentButton.setTitleColor(UIColor.flatWhite(), for: .selected)
addStudentButton.layer.backgroundColor = UIColor.flatForestGreen().cgColor
addStudentButton.layer.shadowRadius = 4.0
addStudentButton.layer.shadowOpacity = 0.8
addStudentButton.layer.shadowOffset = CGSize(width: 0, height: 1)
allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:[addStudentButton(>=44)]", options: [], metrics: nil, views: views)
allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:[addStudentButton(>=44)]", options: [], metrics: nil, views: views)
NSLayoutConstraint.activate(allConstraints)
studentsTableView.addSubview(addStudentButton)
addStudentButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -8.0).isActive = true
addStudentButton.rightAnchor.constraint(equalTo: view.layoutMarginsGuide.rightAnchor, constant: 0.0).isActive = true
I basically have a UITableView that lists students and I want a button on the bottom right of the screen that sits on top of the UITableView that can be selected. The button looks fine until I press it then it never has that selected look where the text changes color and stuff.
The problems are:
let addStudentButton = UIButton()
makes a .custom
button, which does not automatically change when tapped
Your title color is the same for all states, thus specifically preventing any change of title color anyway
A tapped button is highlighted, not selected
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.