【发布时间】:2017-03-13 05:02:32
【问题描述】:
在 UITableView 上添加 UIButton 有什么不同吗?或者是我添加 UIButton 的代码不正确,导致按钮看起来不像被选中
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
我基本上有一个列出学生的 UITableView,我想要一个位于屏幕右下角的按钮,该按钮位于 UITableView 的顶部,可以选择。该按钮看起来很好,直到我按下它然后它永远不会具有文本更改颜色和内容的选定外观。
【问题讨论】:
-
在我看来,如果.selected状态没有自动转,需要手动设置.selected。
-
@nynohu 如何手动设置?我没有不同的背景图片或任何东西...
-
你应该在你的选择器中设置
addStudentButton.selected = !addStudentButton.selected。
标签: ios uitableview uibutton