|
// Hiding NavBar and TabBar example |
|
// Created by Joel Kopelioff on 1/30/15. |
|
// |
|
|
|
import UIKit |
|
import SVProgressHUD |
|
import CoreData |
|
|
|
|
|
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate { |
|
|
|
@IBOutlet weak var tableView: UITableView! |
|
|
|
// Bar showing/hiding on scroll |
|
|
|
var lastContentOffset:CGFloat? |
|
var showBars:Bool = true |
|
|
|
// Ususally goes in a constants file |
|
let DELEG = UIApplication.sharedApplication().delegate as AppDelegate |
|
.... |
|
|
|
//MARK: Status Bar toggle |
|
|
|
override func prefersStatusBarHidden() -> Bool { |
|
return !self.showBars |
|
} |
|
|
|
|
|
//MARK: ScrollView |
|
|
|
func scrollViewDidScroll(scrollView: UIScrollView) { |
|
|
|
let navHeight = self.navigationController?.navigationBar.frame.height |
|
|
|
if scrollView.contentOffset.y < 0 || |
|
scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height || |
|
navHeight == nil || |
|
scrollView.contentSize.height <= scrollView.frame.size.height + navHeight! |
|
{ |
|
return |
|
} |
|
|
|
var show:Bool = true |
|
if self.lastContentOffset > scrollView.contentOffset.y { |
|
show = true |
|
} else if self.lastContentOffset < scrollView.contentOffset.y { |
|
show = false |
|
} |
|
|
|
self.lastContentOffset = scrollView.contentOffset.y; |
|
|
|
if self.showBars != show { |
|
self.showBars = show |
|
self.setNeedsStatusBarAppearanceUpdate() |
|
|
|
self.navigationController?.setNavigationBarHidden(!show, animated: true) |
|
self.setTabBarVisible(show, animated: true) |
|
} |
|
} |
|
|
|
//MARK: Tabbar Visibility |
|
|
|
func setTabBarVisible(visible:Bool, animated:Bool) { |
|
|
|
if DELEG.window?.rootViewController is UITabBarController { |
|
|
|
var tabBarController = DELEG.window?.rootViewController as UITabBarController |
|
|
|
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time |
|
|
|
// bail if the current state matches the desired state |
|
if (tabBarIsVisible(tabBarController) == visible) { return } |
|
|
|
// get a frame calculation ready |
|
let frame = tabBarController.tabBar.frame |
|
let height = frame.size.height |
|
let offsetY = (visible ? -height : height) |
|
|
|
// zero duration means no animation |
|
let duration:NSTimeInterval = (animated ? 0.3 : 0.0) |
|
|
|
// animate the tabBar |
|
UIView.animateWithDuration(duration, animations: { () -> Void in |
|
tabBarController.tabBar.frame = CGRectOffset(frame, 0, offsetY) |
|
}) |
|
} |
|
} |
|
|
|
func tabBarIsVisible(tabBarController:UITabBarController) ->Bool { |
|
return tabBarController.tabBar.frame.origin.y < CGRectGetMaxY(DELEG.window!.frame) |
|
} |
|
} |