这会创建按钮:
var Todaybutton = UIButton(frame: CGRect(x: 0, y: 10, width: view.frame.size.width/7, height: 44))
Todaybutton.setTitle(dayString + "\n\(dayOfWeekString)", for: UIControlState())
Todaybutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.0)
Todaybutton.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: UIControlState())
Todaybutton.titleLabel!.lineBreakMode = .byWordWrapping;
Todaybutton.titleLabel!.numberOfLines = 2
Todaybutton.titleLabel!.textAlignment = .center
Todaybutton.titleLabel?.font = UIFont(name: "Geomanist-Regular", size: 14.0)!
Todaybutton.addTarget(self, action: #selector(Home.onTodayButtonClicked),for: UIControlEvents.touchUpInside)
self.view.addSubview(Todaybutton)
以及按钮操作:
func onTodayButtonClicked(_ sender: UIButton) {
print("button works")
showHUD()
searchTxt.text = ""
sender.setTitleColor(UIColor.black, for: .normal)
Tombutton?.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: .disabled)
TwoDaysbutton?.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: .disabled)
ThreeDaysButton?.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: .highlighted)
FourDaysButton?.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: .reserved)
FiveDaysButton?.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: .selected)
SixDaysButton?.setTitleColor(UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0), for: .normal)
代码sender.setTitleColor(UIColor.black,for:.normal)工作并使按钮文本变黑,但是当点击另一个按钮时它不会变回197/255。我怎么做?