Tuesday, 29 September 2020

Hide/Show Navigation Bar at ViewWillAppear and ViewWillDisappear UI Glitch

so I have 2 VC that I want to set its navigation bar hide when it appears and show again when it disappears. This is my code

class FirstNavigationVCViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "First Nav"
        button.addTarget(self, action: #selector(press), for: .touchUpInside)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    
    @objc func press() {
        navigationController?.pushViewController(SecondNavigationViewController(), animated: true)
    }

}

class SecondNavigationViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Second Nav"
        button.addTarget(self, action: #selector(press), for: .touchUpInside)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    @objc func press() {
        navigationController?.popViewController(animated: true)
    }
}

I assume with that code the navigation bar will not show up when the second vc pop, but instead I got this

enter image description here

do you guys have better solution so I can still hide navigation bar for both vc without that animation?



from Hide/Show Navigation Bar at ViewWillAppear and ViewWillDisappear UI Glitch

No comments:

Post a Comment