こんなのでいいんでしょうかね。
簡単な実装方法かはわかりませんが、TableViewのスクロール量が変わったら、それに合わせてヘッダービュー(?)の座標を調整してあげるだけなのかなと思います。
※iOSのバージョンによって、調整系は若干挙動が変わるので気をつけて実装してください。
storyboard:
ソース:
swift
1import UIKit
2
3class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
4
5 @IBOutlet weak var tableView: UITableView!
6 @IBOutlet weak var header: UILabel!
7
8 override func viewDidLoad() {
9 super.viewDidLoad()
10 // Do any additional setup after loading the view.
11
12 // ヘッダー?分、スクロールできるように確保しておく
13 tableView.contentInset.top += header.frame.height
14 tableView.contentOffset.y += header.frame.height
15 }
16
17 // MARK: - UITableViewDataSource
18
19 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
20 return 100
21 }
22
23 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
24 let cell = UITableViewCell()
25 cell.textLabel?.text = "(indexPath.row)";
26 return cell
27 }
28
29 // MARK: - UIScrollViewDelegate
30
31 func scrollViewDidScroll(_ scrollView: UIScrollView) {
32 // スクロールに合わせてヘッダーの座標を調整する
33 header.frame.origin.y = min(-scrollView.contentOffset.y - header.frame.height, scrollView.adjustedContentInset.top - header.frame.height)
34 // インジケーター(スクロールバー)の表示範囲も調整する ※iOS13未満では別のプロパティだったと思います。
35 scrollView.verticalScrollIndicatorInsets.top = max(header.frame.maxY - (scrollView.adjustedContentInset.top - header.frame.height), 0)
36 }
37}
なお、この式どうなってるの?ときかれても、うまく表示されるように適当に書いたので、あんまり理解はしていません。