buzzfeed/HidingNavigationBar

Name: HidingNavigationBar

Owner: BuzzFeed

Description: Easily hide and show a view controller's navigation bar (and tab bar) as a user scrolls

Forked from: tristanhimmelman/HidingNavigationBar

Created: 2016-09-29 17:59:26.0

Updated: 2016-11-02 06:00:53.0

Pushed: 2017-09-20 22:25:48.0

Homepage:

Size: 2812

Language: Swift

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

HidingNavigationBar

Carthage compatible CocoaPods

An easy to use library (written in Swift) that manages hiding and showing a navigation bar as a user scrolls.

Features

HidingNavigationBar supports hiding/showing of the following view elements:

UINavigationBar

Screenshot

UINavigationBar and an extension UIView

Screenshot

UINavigationBar and a UIToolbar

Screenshot

A UINavigationBar and a UITabBar

Screenshot

Usage

  1. Import HidingNavigationBar
  2. Include a member variable of type HidingNavigationBarManager in your UIViewController subclass.
  3. Initialize the variable in viewDidLoad function, passing in the UIViewController instance and the UIScrollView instance that will control the hiding/showing of the navigation bar.
  4. Relay the following UIViewController lifecycle functions to the HidingNavigationBarManager variable:
    ride func viewWillAppear(animated: Bool)
    ride func viewWillDisappear(animated: Bool)
    ride func viewDidLayoutSubviews() //Only necessary when adding the extension view
    
    And finally relay the following UIScrollViewDelegate function:
     scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool
    

Below is an example of how your UIViewController subclass should look:

rt HidingNavigationBar

s MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var hidingNavBarManager: HidingNavigationBarManager?
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    hidingNavBarManager = HidingNavigationBarManager(viewController: self, scrollView: tableView)
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    hidingNavBarManager?.viewWillAppear(animated)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    hidingNavBarManager?.viewDidLayoutSubviews()
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    hidingNavBarManager?.viewWillDisappear(animated)
}

//// TableView datasoure and delegate 

func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool {
    hidingNavBarManager?.shouldScrollToTop()

    return true
}

...

Note: HidingNavigationBar only works with UINavigationBars that have translucent set to true.

Customization

Add an extension view to the UINavigationBar
extensionView = // load your a UIView to use as an extension
ngNavBarManager?.addExtensionView(extensionView)
Hide and show a UITabBar or UIToolbar
et tabBar = navigationController?.tabBarController?.tabBar {
hidingNavBarManager?.manageBottomBar(tabBar)

Hide/Show/Do Nothing when App is Foregrounded
hidingNavBarManager?.onForegroundAction = .Default  //Do nothing, state of bars will remain the same as when backgrounded
hidingNavBarManager?.onForegroundAction = .Hide     //Always hide on foreground
hidingNavBarManager?.onForegroundAction = .Show     //Always show on foreground
Expansion Resistance

When the navigation bar is hidden, you can some 'resitance' which adds a delay before the navigation bar starts to expand when scrolling. The resistance value is the distance that the user needs to scroll before the navigation bar starts to expand.

ngNavBarManager?.expansionResistance = 150
UIRefreshControl

If you are using a UIRefreshControl with your scroll view, it is important to let the HidingNavigationBarManager know about it:

ngNavBarManager?.refreshControl = refreshControl

Installation

If your using Carthage, add the following line to your Cartfile:

ub "tristanhimmelman/HidingNavigationBar" ~> 1.0

If you are using CocoaPods, add the following line to your Podfile:

pod 'HidingNavigationBar', '~> 1.0'

Otherwise, include the following files directly to your project:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.