UITableView tableFooterView shows at the top of the UITableView - wrong

前端 未结 7 2468
北海茫月
北海茫月 2021-02-08 15:37

I have created a very simple test case to reproduce this issue.

I am trying to set a footer view programmatically to a tableview. Please note that I am referring to the

7条回答
  •  忘掉有多难
    2021-02-08 16:18

    You can use Autolayout and Xibs to create a footer view. But you have to put your custom view to the container view, that you assign to tableFooterView.

    func setupTableFooterView() {
        // Create your footer view from XIB and set constraints (in my case it is historyToolBarView of class HistoryToolBarView)
        let view = Bundle.main.loadNibNamed("HistoryToolBarView", owner: self, options: nil)?.first
        historyToolBarView = view as! HistoryToolBarView
        historyToolBarView.translatesAutoresizingMaskIntoConstraints = false
        historyToolBarView.addConstraints(
            [NSLayoutConstraint.init(item: self.historyToolBarView,
                                     attribute: .height,
                                     relatedBy: .equal,
                                     toItem: nil,
                                     attribute: .notAnAttribute,
                                     multiplier: 1.0,
                                     constant: 60),
             NSLayoutConstraint.init(item: self.historyToolBarView,
                                     attribute: .width,
                                     relatedBy: .equal,
                                     toItem: nil,
                                     attribute: .notAnAttribute,
                                     multiplier: 1.0,
                                     constant: UIScreen.main.bounds.size.width)])
    
        // Create a container of your footer view called footerView and set it as a tableFooterView
        let footerView = UIView(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 60))
        footerView.backgroundColor = UIColor.green
        tableView.tableFooterView = footerView
    
        // Add your footer view to the container
        footerView.addSubview(historyToolBarView)
    }
    

提交回复
热议问题