在UITableView中为Section Header添加填充。

4

我创建了这个部分标题,但不幸的是我不知道如何在右侧添加一些填充。

screenshot

这是我的代码:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let title: UILabel = UILabel()
    title.text = "Days"
    title.backgroundColor = UIColor.lightGrayColor()
    title.textAlignment = NSTextAlignment.Right
    return title
}

这似乎回答了你的问题。另外,在文本末尾添加2个空格怎么样?title.text =“Days ” - shrutim
为什么不尝试创建一个 UIView,将您的 UILabel 作为该视图的子视图,并在标签上使用自动布局约束来添加一些标签侧面的空间? - Wes
https://dev59.com/dGkx5IYBdhLWcg3wAvqn 可以提供帮助。 - Prashant Tukadiya
2个回答

10

你可以像这样做:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
    let label = UILabel(frame: CGRect(x: 15, y: 5, width: tableView.frame.width, height: 20))
    label.text = "Days"
    label.textAlignment = .right
    view.addSubview(label)
    return view
}

1
如何控制多行文本框? - famfamfam

4
我发现在storyboard中进行调整可以解决问题。点击tableView并设置分隔符插入到左边和右边的边缘上。请确保点击tableView而不是cell。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接