Skip to content

Instantly share code, notes, and snippets.

@thomsmed
Last active December 20, 2021 22:17
Show Gist options
  • Select an option

  • Save thomsmed/60be6dd34bfa2beb46c7fb19875080e1 to your computer and use it in GitHub Desktop.

Select an option

Save thomsmed/60be6dd34bfa2beb46c7fb19875080e1 to your computer and use it in GitHub Desktop.
StretchyTableHeaderView.swift - From "Stretchy table view header" @ medium.com
//
// StretchyTableHeaderView.swift
//
import Foundation
import UIKit
import TinyConstraints
class StretchyTableHeaderView: UIView {
internal let contentView: UIView = {
let view = UIView()
view.backgroundColor = .clear
view.clipsToBounds = true
return view
}()
private lazy var contentViewTopConstraint = contentView.topToSuperview()
override init(frame: CGRect) {
super.init(frame: frame)
configureUI()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
configureUI()
}
private func configureUI() {
backgroundColor = .clear
addSubview(contentView)
contentView.leadingToSuperview()
contentView.trailingToSuperview()
contentView.bottomToSuperview()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var offset = min(scrollView.contentOffset.y, 0)
if offset > -scrollView.safeAreaInsets.top {
offset = -scrollView.safeAreaInsets.top
}
contentViewTopConstraint.constant = offset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment