AvsBest
AvsBest

Reputation: 625

TableView footer not in correct location

My first attempt placed the footer in the correct location, however it was too close to the leading margin which made me add 1 constraint to fix the issue. Doing so, caused the footer to be above the header which is obviously not the intended behaviour. All I want to do is add 10 pts to the leading margin from my first attempt. Ideas?

First attempt:

    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()
}

Second attempt:

    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()
}

Upvotes: 0

Views: 317

Answers (1)

Anis Mansuri
Anis Mansuri

Reputation: 343

Try putting this code. Make frame changing according to your requirement.

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
}

Upvotes: 1

Related Questions