Skip to content

Instantly share code, notes, and snippets.

@ericrowe
Created August 2, 2014 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericrowe/3647b46b775fde1b988e to your computer and use it in GitHub Desktop.
Save ericrowe/3647b46b775fde1b988e to your computer and use it in GitHub Desktop.
Hiding the navigationBar and UIToolbar in Swift
// in viewDidLoad...
var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "showToolbars")
tapGestureRecognizer.cancelsTouchesInView = false
scrollView.addGestureRecognizer(tapGestureRecognizer)
// somewhere else...
func showToolbars() {
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.toolbar.frame = CGRectMake(0, self.view.frame.size.height-self.toolbar.frame.size.height, self.toolbar.frame.size.width, self.toolbar.frame.size.height)
if self.ios7Patches!.verticalSizeClass == UIUserInterfaceSizeClass.Compact { // iPhone Horizontal
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)
} else {
self.navigationController.navigationBar.frame = CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)
}
}, completion: {(value: Bool) in})
hideToolbarsTimer?.invalidate()
hideToolbarsTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "hideToolbars", userInfo: nil, repeats: false)
}
func hideToolbars() {
hideToolbars(nil, delay: nil)
}
func hideToolbars(duration:NSTimeInterval?, delay:NSTimeInterval?) {
var intDelay : NSTimeInterval
if var thisDelay = delay {
intDelay = thisDelay
} else {
intDelay = 0.0
}
var intDuration : NSTimeInterval
if var thisDuration = duration {
intDuration = thisDuration
} else {
intDuration = 1.0
}
UIView.animateWithDuration(intDuration, delay: intDelay, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.toolbar.frame = CGRectMake(0, self.view.frame.size.height, self.toolbar.frame.size.width, self.toolbar.frame.size.height)
self.navigationController.navigationBar.frame = CGRectMake(0, -(self.navigationController.navigationBar.frame.size.height), self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)
}, completion: {(value: Bool) in})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment