【问题标题】:Swift button only showing in dark modeSwift 按钮仅在暗模式下显示
【发布时间】:2021-01-19 05:20:53
【问题描述】:

我在 tableview 部分标题中添加了一个按钮,它目前仅在我的手机处于暗模式时显示,我不知道问题可能是什么。标题中的所有其他元素都以明暗模式显示。

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, width: tableView.frame.width, 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
}

【问题讨论】:

  • 没有设置一个,我创建的另一个按钮在明暗模式下都显示得很好
  • 当我不将其设置为 .label color 时会发生同样的事情
  • 刚刚发布了整个功能

标签: ios swift uitableview uibutton


【解决方案1】:

问题出在这一行:

nameButton.titleLabel?.textColor = .label

这不是您设置按钮标题颜色的方式。将其更改为:

nameButton.setTitleColor(.label, for: .normal)

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多