Ios 在UITableView顶部按下UIButton时,UIButton不会更改其状态

Ios 在UITableView顶部按下UIButton时,UIButton不会更改其状态,ios,uitableview,uibutton,Ios,Uitableview,Uibutton,在UITableView顶部添加UIButton时是否有不同之处?或者我添加UIButton的代码不正确,导致按钮看起来不像被选中了 let addStudentButton = UIButton() addStudentButton.translatesAutoresizingMaskIntoConstraints = false let views = ["addStudentButton": addStudentButton] var allConstrai

在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的顶部,可以选择。在我按下按钮之前,按钮看起来很好,但在文本改变颜色和内容的情况下,它从来没有选定的外观。

问题在于:

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

  • 所有州的标题颜色都相同,因此特别防止标题颜色的任何更改

  • 点击的按钮高亮显示,未选中


在我看来,如果.selected状态没有自动转换,您需要手动设置.selected。@nynohu如何手动设置?我没有不同的背景图像或任何东西…您应该设置
addStudentButton.selected=!addStudentButton。在选择器中选择