UITableView - 部分的页脚 - iPhoneX

时间:2017-12-30 14:36:27

标签: ios uitableview

我的iPhone和UITableView部分页脚有问题。它正在隐藏"在顶部切割部分后面。

我有这段代码:

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        var content = "some long text"


        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 40))

        let explanationLabel = UILabel(frame: CGRect(x: 10, y: 0, width: view.frame.size.width - 20, height: 50))
        explanationLabel.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.light)
        explanationLabel.textColor = UIColor.darkGray
        explanationLabel.numberOfLines = 0
        explanationLabel.lineBreakMode = .byTruncatingTail
        explanationLabel.text = content
        footerView.addSubview(explanationLabel)
        return footerView
    }

我从故事板中添加了UITableViewController,并设置了安全区域。我对表格行没有问题,只有章节页脚。

1 个答案:

答案 0 :(得分:0)

似乎我已经解决了此代码的问题,在footerView.addSubview(explanationLabel)之后添加:

explanationLabel.translatesAutoresizingMaskIntoConstraints = false

if #available(iOS 11.0, *) {
    let guide = footerView.safeAreaLayoutGuide

    NSLayoutConstraint.activate([
        explanationLabel.topAnchor.constraint(equalTo: footerView.topAnchor),
        explanationLabel.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 10),
        explanationLabel.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -10),
        explanationLabel.bottomAnchor.constraint(equalTo:  footerView.bottomAnchor)
   ])
}
else {        
    NSLayoutConstraint.activate([
        explanationLabel.topAnchor.constraint(equalTo: footerView.topAnchor),
        explanationLabel.leadingAnchor.constraint(equalTo: footerView.leadingAnchor, constant: 10),
        explanationLabel.trailingAnchor.constraint(equalTo: footerView.trailingAnchor, constant: -10),
        explanationLabel.bottomAnchor.constraint(equalTo:  footerView.bottomAnchor)
    ])
}