Skip to content

Instantly share code, notes, and snippets.

@aal89
Created October 22, 2018 07:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aal89/1736f17047f077959b3447a31bb87233 to your computer and use it in GitHub Desktop.
Save aal89/1736f17047f077959b3447a31bb87233 to your computer and use it in GitHub Desktop.
Simple stretchy header example for Swift.
//
// ViewController.swift
// stretchy header example
//
// Created by Alex on 07/03/2018.
// Copyright © 2018. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var tableView: UITableView!
let STATUS_BAR_HEIGHT: CGFloat = 20.0
let MAX_HEIGHT_HEADER: CGFloat = 300.0
let MIN_HEIGHT_HEADER: CGFloat = 64.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.contentInset = UIEdgeInsets(top: MAX_HEIGHT_HEADER - STATUS_BAR_HEIGHT, left: 0, bottom: 0, right: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let y = -scrollView.contentOffset.y
let height = min(max(y, MIN_HEIGHT_HEADER), MAX_HEIGHT_HEADER)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment