UITutton在UITableView顶部按下时不会改变其状态

时间:2017-03-13 05:02:33

标签: ios uitableview uibutton

在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顶部的屏幕右下方可以选择的按钮。按钮看起来很好,直到我按下它然后它从来没有选择的外观文本改变颜色和东西。

1 个答案:

答案 0 :(得分:0)

问题是:

  • let addStudentButton = UIButton()生成一个.custom按钮,点按时不会自动更改

  • 您的标题颜色对于所有州都是相同的,因此特别防止标题颜色发生任何变化

  • 点按的按钮突出显示,未选中