Skip to content

Instantly share code, notes, and snippets.

@automactic
Last active September 12, 2016 03:57
Show Gist options
  • Save automactic/5a82d2c6cfd4515238659113cfd6d487 to your computer and use it in GitHub Desktop.
Save automactic/5a82d2c6cfd4515238659113cfd6d487 to your computer and use it in GitHub Desktop.
var navBarOriginalHeight: CGFloat = 0.0
let navBarMinHeight: CGFloat = 10.0
var previousScrollViewYOffset: CGFloat = 0.0
// MARK: - UIScrollViewDelegate
func scrollViewDidScroll(scrollView: UIScrollView) {
guard scrollView.contentSize.height >= scrollView.frame.height else {return}
guard let navigationBar = navigationController?.navigationBar else {return}
guard scrollView.contentOffset.y > 100 else {return}
// Calculate current YOffset but without elasticity
let currentScrollViewYOffset: CGFloat = {
let topInset = scrollView.contentInset.top
let bottomInset = scrollView.contentInset.bottom
let minYOffset = -topInset
let maxYOffset = scrollView.contentSize.height + bottomInset - scrollView.frame.height
return max(minYOffset, min(scrollView.contentOffset.y, maxYOffset))
}()
// delta content offset y, scroll up minus, scroll down plus
let yDelta = previousScrollViewYOffset - currentScrollViewYOffset
// Slide up nav bar
let navOriginY = max(20.0 - 44.0, min(navigationBar.frame.origin.y + yDelta, 20.0))
let navFrame = CGRectMake(0, navOriginY, navigationBar.frame.width, navigationBar.frame.height)
navigationBar.frame = navFrame
// Slide down tool bar
if let toolBar = navigationController?.toolbar {
let originY = max(view.frame.height - 44.0, min(toolBar.frame.origin.y - yDelta, view.frame.height))
let frame = CGRectMake(0, originY, toolBar.frame.width, toolBar.frame.height)
toolBar.frame = frame
}
// Shrink nav bar
//let newNavBarHeight = max(navBarMinHeight, min(navigationBar.frame.height + yDelta, 44.0))
//let navFrame = CGRectMake(0, navigationBar.frame.origin.y, navigationBar.frame.width, newNavBarHeight)
//navigationBar.frame = navFrame
updateNavBarItems()
configureWebViewInsets()
previousScrollViewYOffset = currentScrollViewYOffset
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
stoppedScrolling()
}
func updateNavBarItems() {
guard let navigationBar = navigationController?.navigationBar else {return}
let min: CGFloat = 20.0 - 44.0
let max: CGFloat = 20.0
let alpha = (navigationBar.frame.origin.y - min) / (max - min)
navigationItem.titleView?.alpha = alpha
}
func stoppedScrolling() {
guard let navigationBar = navigationController?.navigationBar else {return}
let show = ((navigationBar.frame.origin.y - (20-44)) / 44) > 0.5
animateBar(show)
}
func animateBar(show: Bool) {
UIView.animateWithDuration(0.2) { () -> Void in
if show {
if let navBar = self.navigationController?.navigationBar {
navBar.frame = CGRectMake(0, 20, navBar.frame.width, navBar.frame.height)
}
if let toolBar = self.navigationController?.toolbar {
toolBar.frame = CGRectMake(0, self.view.frame.height - toolBar.frame.height, toolBar.frame.width, toolBar.frame.height)
}
self.navigationItem.titleView?.alpha = 1.0
} else {
if let navBar = self.navigationController?.navigationBar {
navBar.frame = CGRectMake(0, 20 - 44, navBar.frame.width, navBar.frame.height)
}
if let toolBar = self.navigationController?.toolbar {
toolBar.frame = CGRectMake(0, self.view.frame.height, toolBar.frame.width, toolBar.frame.height)
}
self.navigationItem.titleView?.alpha = 0.0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment