Skip to content

Instantly share code, notes, and snippets.

@ccabanero
Last active November 30, 2022 03:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ccabanero/7c47bcb06573a17f9858 to your computer and use it in GitHub Desktop.
Save ccabanero/7c47bcb06573a17f9858 to your computer and use it in GitHub Desktop.
Fade Out TitleView on Swipe
/**
For configuring the NavigationBar to show/hide when user swipes
- returns: void
*/
func configureNavigationBarAsHideable() {
if let navigationController = self.navigationController {
// respond to swipe and hide/show
navigationController.hidesBarsOnSwipe = true
// get the pan gesture used to trigger hiding/showing the NavigationBar
let panGestureRecognizer = navigationController.barHideOnSwipeGestureRecognizer
// when the user pans, call action method to fade out title view
panGestureRecognizer.addTarget(self, action:"fadeOutTitleView:")
}
}
/**
For fading out the title view
- parameter sender: The UIPanGestureRecognizer when user swipes (and hides/shows NavigationBar))
- returns: void
*/
func fadeOutTitleView(sender: UIPanGestureRecognizer) {
if let titleView = self.navigationItem.titleView {
// fade out title view when swiping up
let translation = sender.translationInView(self.view)
if(translation.y < 0) {
let alphaValue = 1 - abs(translation.y / titleView.frame.height)
titleView.alpha = alphaValue
}
// clear transparency if the Navigation Bar is not hidden after swiping
if let navigationController = self.navigationController {
let navigationBar = navigationController.navigationBar
if navigationBar.frame.origin.y > 0 {
if let titleView = self.navigationItem.titleView {
titleView.alpha = 1
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment