【问题标题】:cannot add UIButton to tableView section无法将 UIButton 添加到 tableView 部分
【发布时间】:2019-09-07 18:15:20
【问题描述】:

我有这个代码

 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 没有显示标签显示的原因。

【问题讨论】:

  • 定义“不工作”。请edit您的问题提供有关此代码究竟以何种方式没有达到您期望的具体细节。不要在评论中回复。
  • 该按钮是否存在但处于非活动状态或者甚至不可见?
  • 工作正常。我给按钮一个绿色背景,它清楚地显示在模拟器中。我启用了目标选择器,它也可以工作。我已经投票关闭。
  • @BartvanKuik 按钮文字颜色怎么样?

标签: ios swift uitableview tableview


【解决方案1】:

这行不行:

button.titleLabel?.textColor = UIColor.black

将其替换为以下行:

button.setTitleColor(.black, for: .normal)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多