【问题标题】:UIButton does not change its state when pressed when on top of UITableView在 UITableView 顶部按下时,UIButton 不会更改其状态
【发布时间】: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


【解决方案1】:

问题是:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2018-01-21
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多