Last active
December 20, 2021 22:17
-
-
Save thomsmed/60be6dd34bfa2beb46c7fb19875080e1 to your computer and use it in GitHub Desktop.
StretchyTableHeaderView.swift - From "Stretchy table view header" @ medium.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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