How to scroll up an entire table view in Swift?

2019-08-03 17:50发布

I have a horizontal scrolling paged collectionview on top, and a tableview at the bottom, like this:

enter image description here

What I'm trying to achieve is that when the user scrolls up the table view, I want the whole page to scroll upwards. Currently, the tableview is confined to the bottom space when scrolling up. Lots of apps have this pattern, but the Fiverr app is an example, and here is a screenshot. The second picture is when I started to scroll up:

enter image description here

I am a complete beginner in Swift so I have no idea even what keywords to search for. What would be the easiest way to accomplish this?

1条回答
Evening l夕情丶
2楼-- · 2019-08-03 18:13

I think you want to put all the views into a UIScrollView.

You can also put the UICollectionView into the table's header view. But if you have more components below the UITableView, I find it easier to just put the whole thing into a UIScrollView.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    //build header view here
    headerView.addSubview(collectionView)
    return headerView
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    //Return the height of the headerview.
    return height
}

Edited to answer comment :)

查看更多
登录 后发表回答