How can I make the fixed cell at the bottom of the

2019-07-24 13:53发布

问题:

I would like to make a fixed cell at the bottom of the table view in iOS similar to that of leaderboard view of HQ trivia iOS app.

Like this:

回答1:

To give the impression of a fixed cell, you can simply add a UITableView onto a regular UIViewController, set its constraints so that it consumes the whole view, but stops (for example) 60px from the bottom of the screen.

Fill the remaining space with a UIView that has the same UI as the cell ... and thats it, the tableview will scroll, and the "cell" will always be visible at the bottom.



回答2:

I believe the easiest and cleanest way to achieve that would be to add a "cell"-like subview to the main view of the viewController and use autolayout constraints to make it fixed under the tableView:

NSLayoutConstraint.activate([
    tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
    tableView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
    tableView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
    // this will make sure tableView will be placed above bottomFixedFooter, and bottomFixedFooter won't overlay any content of the tableView
    bottomFixedFooter.topAnchor.constraint(equalTo: tableView.bottomAnchor),

    bottomFixedFooter.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
    bottomFixedFooter.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
    bottomFixedFooter.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),

    // explicitly set the height of the bottomFixedFooter, if it does not have intrinsic content size (or its size is not calculated
    // internally using autolayout)
    bottomFixedFooter.heightAnchor.constraint(equalToConstant: 50),
    ])


回答3:

As Simon mentions, I put footer view at the bottom of the scroll view. I leave space for footer view so that it is fixed at the bottom with constraints.

Example