TableView页脚不在正确的位置

时间:2020-09-13 23:53:44

标签: ios swift uitableview footer

我的第一次尝试将页脚放置在正确的位置,但是它太靠近前缘,这使我增加了1个约束来解决此问题。这样做导致页脚位于页眉上方,这显然不是预期的行为。我要做的就是从我的第一次尝试中将10点添加到领先保证金中。想法?

第一次尝试:

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

    if section == 0 {
    
        let label = UILabel()
        label.text = "Your birth month is not set"
        label.font = UIFont.preferredFont(forTextStyle: .footnote)
        label.textColor = UIColor.gray
                    
        return label
    }
    
    return UIView()
}

第二次尝试:

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

    if section == 0 {
    
        let label = UILabel()
        view.addSubview(label)
        
        label.translatesAutoresizingMaskIntoConstraints = false
        let constraints = label.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: 10)
        NSLayoutConstraint.activate([constraints])
        
        label.text = "Your birth month is not set"
        label.font = UIFont.preferredFont(forTextStyle: .footnote)
        label.textColor = UIColor.gray
                    
        return label
    }
    
    return UIView()
}

1 个答案:

答案 0 :(得分:1)

尝试输入此代码。根据您的要求更改框架。

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let headerView = UIView(CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
    let lblDate = UILabel(frame: CGRect(x: 10, y: 0, width: headerView.frame.width - 10, height: 50))
    headerView.addSubview(lblDate)
    lblDate.text = "text"
    return headerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 50
}