     
     // MARK: - Properties
     
-    fileprivate var baseHeight: CGFloat!
+    fileprivate var initialHeight: CGFloat!
     
     
     
     // MARK: - Public Functions
     
     open func handleScroll(in scrollView: UIScrollView) {
+        clipsToBounds = true
+        setup(in: scrollView as? UITableView)
+        guard initialHeight != nil else { return }
         frame = getNewFrame(in: scrollView)
     }
     
     open func setup(in tableView: UITableView?) {
+        guard initialHeight == nil else { return }
         guard let tableView = tableView else { return }
         guard self == tableView.tableHeaderView else { return }
         let height = frame.size.height


/*
 
 This view will be stretched when the user pans a scroll
 view in the negative scroll direction, then scroll with
 the scroll view when its content is scrolled.
 
 To use this view with a table view, first add it as the
 table header view. Then, call setup(in:) in viewDidLoad.
 Finally call handleScroll(in:) in scrollViewDidScroll(:).
 
 */

import UIKit

open class StretchableHeaderView: UIView {

    
    // MARK: - Initialization
    
    public convenience init() {
        self.init(frame: CGRect.zero)
        setup()
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()

fileprivate extension StretchableHeaderView {
    
    func isStretching(in scrollView: UIScrollView) -> Bool {
        return scrollView.contentOffset.y < -initialHeight
    }
    
    func getNewFrame(in scrollView: UIScrollView) -> CGRect {
        let width = scrollView.bounds.width
        var rect = CGRect(x: 0, y: -initialHeight, width: width, height: initialHeight)
        guard isStretching(in: scrollView) else { return rect }
        rect.origin.y = scrollView.contentOffset.y
        rect.size.height = -scrollView.contentOffset.y
        return rect
    }
}
