Accueil

Masquer/Afficher la Navigation Bar

Tutoriel Swift

Posté par Véronique le 25 February 2020

Voici 3 méthodes pour gérer l'affichage d'une NavigationBar dans un ViewController.

1. NavigationBar masquée dans un ViewController

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Hide the Navigation Bar
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the Navigation Bar
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

2. NavigationBar masquée uniquement en orientation portrait

 override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Show the Navigation Bar
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if (UIDevice.current.orientation.isLandscape) {
        // Hide the Navigation Bar
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    } else {
        // Show the Navigation Bar
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
}

3. NavigationBar affichée/masquée grâce au tap gesture recognizer

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.hidesBarsOnTap = true
}

Plus d'info sur la documentation Apple développeur.