问题描述
我在表视图部分标题中添加了一个按钮,该按钮当前仅在手机处于黑暗模式时显示,我不知道可能是什么问题。标头中的所有其他元素均以亮和暗模式显示。
headerHeight
是静态200像素
override func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0,y: 0,width: tableView.frame.width,height: headerHeight))
let imageButton = UIButton(frame: CGRect(x: view.frame.width / 2 - 50,y: 10,width: 100,height: 100))
imageButton.setimage(UIImage(systemName: "person.crop.circle"),for: .normal)
view.addSubview(imageButton)
let nameButton = UIButton(frame: CGRect(x: 0,y: 110,height: 40))
nameButton.tintColor = .label
nameButton.titleLabel?.textColor = .label
nameButton.setTitle("Set Name",for: .normal)
nameButton.titleLabel?.textAlignment = .center
view.addSubview(nameButton)
let accountsLabel = UILabel(frame: CGRect(x: 10,y: 175,width: 80,height: 20))
accountsLabel.text = "Accounts"
accountsLabel.font = UIFont(name: "Helvetica Neue",size: 15)
view.addSubview(accountsLabel)
return view
}
解决方法
问题是这一行:
nameButton.titleLabel?.textColor = .label
这不是您设置按钮标题颜色的方式。更改为:
nameButton.setTitleColor(.label,for: .normal)