Search code examples
iosswiftautolayoutconstraints

Adding footer to UITableView


So, I made a UITableView, with custom cell's (.xib files), that works well. The problem is that I now want to add a footer, that needs to have a UIImageView and a UILabel.

This is what I'm currently doing: (of course I'm executing this function inside viewDidLoad() )

private func setupTableViewFooter() {
        /// Insight Logo
        let myAppLogo = UIImageView()
        myAppLogo.backgroundColor = .clear
        myAppLogo.widthAnchor.constraint(equalToConstant: 110.0).isActive = true
        myAppLogo.heightAnchor.constraint(equalToConstant: 60.0).isActive = true
        myAppLogo.contentMode = .scaleAspectFit
        myAppLogo.image = UIImage(named: "myAppLogo")
        
        /// Description Label
        let label = UILabel()
        label.backgroundColor = .clear
        label.widthAnchor.constraint(equalToConstant: self.view.frame.size.width - 60).isActive = true
        label.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
        label.textAlignment = .left
        label.textColor = K.Colors.black
        label.font = UIFont(name: "Roboto-Medium", size: 13)
        label.text = "Hello world!"
        
        /// Stack View
        let footer = UIStackView()
        footer.axis = .vertical
        footer.distribution = .fill
        footer.alignment = .leading
        footer.spacing = 00.0
        footer.backgroundColor = .lightGray
        
        footer.addArrangedSubview(myAppLogo)
        footer.addArrangedSubview(label)
        footer.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30),
            footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30),
            footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60),
            footer.heightAnchor.constraint(equalToConstant: 90)
        ])
        
        self.tableView.tableFooterView = footer
    }

I get this error:

libc++abi.dylib: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x282fa0640 "UIStackView:0x108022090.left"> and <NSLayoutXAxisAnchor:0x282f56400 "UIView:0x108020ec0.left"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.' terminating with uncaught exception of type NSException

I'm not used to set constraints programmatically, so it's getting really hard for me to solve this. Please help!


Solution

  • first add the fotter to tableView then add the Constraints

    100% ok tested

    self.tableView.tableFooterView = footer
    
    NSLayoutConstraint.activate([ footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30), footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30), footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60), footer.heightAnchor.constraint(equalToConstant: 90) ])