簡體   English   中英

在UITableView上方按下時,UIButton不會更改其狀態

[英]UIButton does not change its state when pressed when on top of UITableView

在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頂部。 該按鈕看起來不錯,直到我按下它為止,然后它再也沒有看到文本更改顏色和填充物的所選外觀。

問題是:

  • let addStudentButton = UIButton()創建一個.custom按鈕,該按鈕在點擊時不會自動更改

  • 您的標題顏色在所有州都是相同的,因此專門可以防止標題顏色發生任何變化

  • 輕擊的按鈕突出顯示,未選中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM