Search code examples
iosswiftheadertableviewfooter

How do I keep my section footer stick at the end of one specific footer in table view swift


I have some sections here and on section 1, I have this footer as an additional information. But it keeps floating around when I scroll the screen. How do I stop that? How do I keep it under the section? I'm using viewForFooterInSection and here's my code :

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    if section == 1 {
        let label = UILabel()
        label.text = "A sticky footer here..."
        label.font = .systemFont(ofSize: 16)
        label.textColor = UIColor.gray
        label.backgroundColor = UIColor.clear
        label.textAlignment = .left
        footerView.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.topAnchor.constraint(equalTo: footerView.topAnchor, constant: 10).isActive = true
        label.leftAnchor.constraint(equalTo: footerView.leftAnchor, constant: 20).isActive = true
        label.rightAnchor.constraint(equalTo: footerView.rightAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: footerView.bottomAnchor).isActive = true
    }
    return footerView
}

I want to keep it sticky, under my section 1. How can I do that?

Thankyou in Advance

enter image description here


Solution

  • You should initialize your table view with the grouped style.

    let tableView = UITableView(frame: .zero, style: .grouped)