How can I make the fixed cell at the bottom of the table view in iOS?

杀马特。学长 韩版系。学妹 提交于 2020-01-04 05:37:06

问题


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



来源:https://stackoverflow.com/questions/49033132/how-can-i-make-the-fixed-cell-at-the-bottom-of-the-table-view-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!