我有这个代码
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 45))
headerView.backgroundColor = UIColor.white
let label = UILabel(frame: CGRect(x: 0, y: 12.5, width: tableView.frame.width - 100, height: 20))
label.numberOfLines = 0
label.font = UIFont(name: "SFProDisplay-Bold", size: 18);
label.textColor = UIColor.darkGray
let sectionName: String
switch section {
case 0:
sectionName = NSLocalizedString(" Featured", comment: "featured")
case 1:
sectionName = NSLocalizedString(" Trending", comment: "trending")
// ...
default:
sectionName = NSLocalizedString(" New", comment: "new")
}
label.text = sectionName
label.adjustsFontForContentSizeCategory = true
headerView.addSubview(label)
let button:UIButton = UIButton(
frame: CGRect(x: tableView.bounds.size.width-100,
y: 12.5,
width: 100,
height: 20))
button.setTitle("More", for: .normal)
button.titleLabel?.textColor = UIColor.black
button.backgroundColor = UIColor.white
// button.addTarget(self, action: #selector(btnShowHideTapped), for: .touchUpInside)
button.titleLabel?.textColor = UIColor.black
headerView.addSubview(button)
return headerView
}
我试图将UIButton添加到tableView标头中,但是它不起作用。有人知道为什么吗?
UIButton不显示标签为什么显示。
答案 0 :(得分:0)
此行不起作用:
button.titleLabel?.textColor = UIColor.black
将其替换为以下行:
button.setTitleColor(.black, for: .normal)