Created
October 22, 2018 07:47
-
-
Save aal89/1736f17047f077959b3447a31bb87233 to your computer and use it in GitHub Desktop.
Simple stretchy header example for Swift.
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
// | |
// 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