theoadahl
theoadahl

Reputation: 524

Swift: Hidden navigation bar appears when tap occurs in view

Introduction

I'm creating a simple app in with the RootViewController is embedded in a UINavigationController. I have a UIView subclass "landscapeView" with a UICollectionView in it that fills the view. "landscapeView" is hidden in portrait and displayed in landscape device orientation.

Issue

When the device is rotated to a landscape orientation I hide the navigationBar and the portrait table view "rootTableView", while showing the "landscapeView". However, the navigationBar appears when I tap the screen in landscape orientation. I can't figure out how to disable this tap to show thing. (I have `navigationController?.hidesBarsOnTap = false, its setup to default in storyboard).

Clarification: hiding the navigation bar works perfectly depending on device orientation.

Question

How can I prevent the navigationBar from appearing when the screen is tapped in landscape orientation?

Code

  1. viewWillTransition() in the "RootViewController"

    private let landscapeView = LandscapeView(frame: .zero)
    private let rootTableView = UITableView(frame: .zero, style: .grouped)
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)   
    
        var isLandscape = true
        switch UIDevice.current.orientation {
        case .landscapeLeft:
            navigationController?.navigationItem.searchController?.accessibilityElementsHidden = true
    
            isLandscape = true
        case .landscapeRight:
    
            navigationController?.navigationItem.searchController?.accessibilityElementsHidden = true
    
            isLandscape = true
        case .portrait, .portraitUpsideDown, .faceUp, .faceDown, .unknown:
    
            isLandscape = false
          navigationController?.navigationItem.searchController?.accessibilityElementsHidden = false
    
        default:
            break
        }
    
        if isLandscape {
            navigationController?.setNavigationBarHidden(true, animated: false)
            self.landscapeView.isHidden = false
    
            // This simply tells the "landscapeView" to layoutSubviews() and reloadData() for the collectionView within.
            landscapeViewDelegate?.landscapeViewWillAppear(inDarkMode: inDarkMode)
        }
        UIView.animate(withDuration: 0.6, delay: 0, options: .layoutSubviews, animations: {
            // This is a tableView displayed in portrait mode.
            self.rootTableView.alpha = isLandscape ? 0 : 1 
            self.landscapeView.alpha = isLandscape ? 1 : 0
        }) { (success) in
            if isLandscape == false {
                self.landscapeView.isHidden = true
                 self.navigationController?.setNavigationBarHidden(false, animated: true)
            }
            return
        }
    }
    

Thanks for reading.

Upvotes: 1

Views: 1448

Answers (1)

André Slotta
André Slotta

Reputation: 14040

The issue is that you checked Hide Bars When Vertically Compact for your navigation controller in the storyboard. This property brings that tap behaviour with it:

When the value of this property is true, the navigation controller hides its navigation bar and toolbar when it transitions to a vertically compact environment. Upon returning to a vertically regular environment, the navigation controller automatically shows both bars again. In addition, unhandled taps in the content area cause the navigation controller to show both bars again. The default value of this property is false.

You can uncheck that property since you take care of showing and hiding the navigation bar in the viewWillTransition method yourself.

Upvotes: 2

Related Questions