Giter Club home page Giter Club logo

hidenavigationbarindifferentviewcontrollers's Introduction

DEPRECATED!


HideNavigationBarInDifferentViewControllers

Use UINavigationControllerDelegate to hide navigation bar in different view controllers.

Assume that you have many kinds of view controllers. Some of them need navigation bar and others don't. A common solution is to call setNavigationBarHidden in each view controller. Like below:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.setNavigationBarHidden(true/false, animated: animated)
}

And hide navigation bar when view controller disappears:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    
    if isMovingFromParentViewController() // or other logic
    {
        navigationController?.setNavigationBarHidden(wasNavigationBarHidden, animated: animated)
    }
}

But there're a few problems using this solution when it comes to a situation like this:

HideNavigationBarViewController1 is the root view controller in a navigation stack. HideNavigationBarViewController1 can push to HideNavigationBarViewController2 and ShowNavigationBarViewController1. HideNavigationBarViewController2 and ShowNavigationBarViewController1 can push to other view controllers just like HideNavigationBarViewController1 do.

It's very complicated to call setNavigationBarHidden every now and then. When two view controllers of hide-navigation-bar-view-controller come along, the navigation bar will flash when push and pop.

--

UINavigationControllerDelegate is very helpful to do a such thing! EASY.

extension Xxx: UINavigationControllerDelegate {
    
    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {

        switch viewController {
        
        case viewController as HideNavigationBarViewController1, viewController as HideNavigationBarViewController2, viewController as HideNavigationBarViewController3:
            navigationController.setNavigationBarHidden(true, animated: animated)
            
        default:
            navigationController.setNavigationBarHidden(false, animated: animated)
        }
    }

}

And one more step to enable the interactive pop gesture that is disabled by setNavigationBarHidden.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    // http://stackoverflow.com/a/28919337/1573750
    // Do this in the root view controller of a navigation controller
    navigationController?.interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
}

hidenavigationbarindifferentviewcontrollers's People

Contributors

jaybowang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.