【问题标题】:How to manage textlabel height by parent view如何通过父视图管理文本标签高度
【发布时间】:2017-12-16 14:40:31
【问题描述】:

我有一些问题。我尝试制作自定义表格标题部分。这是我的代码:

   func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 36))

        sectionHeader.backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1)

        tableView.backgroundColor = colors.background

        let headerName = UILabel(frame: CGRect(x: 15, y: 0, width: tableView.frame.size.width, height: sectionHeader.frame.size.height))

        headerName.font = UIFont(name: "Helvetica", size: 12)
        headerName.text = sectionHeaders[section]
        headerName.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
        headerName.clipsToBounds = true

        sectionHeader.addSubview(headerName)

        return sectionHeader
    }

结果如下:

我用蓝色和黄色标记并注意到,标签高度与 headerview 不同。有人可以帮助我如何管理文本标签高度到标题视图高度,以及如何垂直对齐文本

【问题讨论】:

  • 你用过func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)吗?

标签: ios swift uitableview


【解决方案1】:

标签高度与headerview不同

首先你应该设置节标题的高度:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 36.0
}

基于来自let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 36))height: 36

如何垂直对齐文本

headerName.textAlignment = .center

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2021-10-25
    • 2015-01-30
    相关资源
    最近更新 更多