Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differance b/w the way of hiding nav bar in iOS swift

Tags:

Recently i had one issue. When in my navigation controller stack (One child controller) :

MainScreen -> ScreenA -> ScreenB -> Screenc

So when in my Screenc i want to hide nav bar and status bar.Its was working fine. But when i come back to my ScreenB my status bar and nav bar are overlap.Not sure why its happend.I search in some google and some SO answers. Then i got that isNavigationBarHidden base is UIViewController while isHidden base is UIView.

Why i hide my both nav bar and status bar with below code :

 override var prefersStatusBarHidden: Bool {
        return true
    }
 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
       self.navigationController?.navigationBar.isHidden = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
       self.navigationController?.navigationBar.isHidden = false
    }

In my prev screen i got the issues of my status bar and nav bar are got overlapeed.

But when i use this code :

 override var prefersStatusBarHidden: Bool {
            return true
        }
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.isNavigationBarHidden = false
    }

Its work well. There is not overlap in my prev screen. Why its happening. Is there any specific reason behid :

navigationController?.isNavigationBarHidden ( vs )self.navigationController?.navigationBar.isHidden

I am very to the iOS Development. Just want to understand the difference. So that i can learn if i am doing any wrong.

Thanks

like image 730
kumar barian Avatar asked Jun 18 '20 11:06

kumar barian


1 Answers

Yes, there is a huge difference. Very good point!

  • The first one rudely and illegally reaches into the nav controller’s interface and manipulates it directly. You should never interfere with any other view controller's interface directly: not Cocoa's, not your own. Only a view controller should control its own interface.

  • The second one politely and correctly instructs the nav controller how to behave. It is the "public API" for showing and hiding the navigation bar. — Actually the correct approach is to call setNavigationBarHidden(_:animated:), but setting isNavigationBarHidden calls that for you.

like image 176
matt Avatar answered Oct 12 '22 01:10

matt