首页 > 解决方案 > 缩进表格中的标题

问题描述

我有一张有两个部分的桌子。标题部分的属性textAligment = .right。但它离手机的边框太近了。如何从靠近中心的边界缩进?

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        let header = view as! UITableViewHeaderFooterView

        header.textLabel?.textColor = .black
        header.textLabel?.textAlignment = .right

    }

标签: iosswiftuitableviewheaderswift5

解决方案


这是一个非常简单的例子......

  • 在情节提要中添加一个UITableViewController
  • 将其自定义类分配给SectionTableViewController(来自下面的代码)。
  • 使用默认单元格。
  • 给单元格一个标识符“DefCell”

运行应用程序。这是你应该看到的:

在此处输入图像描述

我已经限制标签使用contentView.layoutMarginsGuide并将尾随常量设置为-32

class MySectionHeaderView: UITableViewHeaderFooterView {

    let myLabel: UILabel = {
        let v = UILabel()

        v.translatesAutoresizingMaskIntoConstraints = false
        v.textColor = .black
        v.textAlignment = .right

        // during development, give it a background color to make it easy to see the frame
        //v.backgroundColor = .cyan

        return v
    }()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() -> Void {

        contentView.addSubview(myLabel)

        // use layout margins guide
        let g = contentView.layoutMarginsGuide

        NSLayoutConstraint.activate([
            myLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
            myLabel.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
            myLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),

            // set the constant to provide more trailing space as desired
            // Note: must be a negative value
            myLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -32.0),
        ])

    }

}

class SectionTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(MySectionHeaderView.self, forHeaderFooterViewReuseIdentifier: "MyHeader")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let c = tableView.dequeueReusableCell(withIdentifier: "DefCell", for: indexPath)
        c.textLabel?.text = "Section: \(indexPath.section) Row: \(indexPath.row)"
        return c
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let v = tableView.dequeueReusableHeaderFooterView(withIdentifier: "MyHeader") as? MySectionHeaderView else {
            fatalError("Could not dequeue MySectionHeaderView!")
        }
        v.myLabel.text = "Section \(section)"
        return v
    }

}

您会在 in 的声明中注意到这些myLabelMySectionHeaderView

    // during development, give it a background color to make it easy to see the frame
    //v.backgroundColor = .cyan

这是标签具有背景颜色时的外观,因此您可以轻松查看标签的布局方式:

在此处输入图像描述


推荐阅读