I have 3 ViewControllers like below.
A: HomeController, B: NavController1, C: NavController2
I set a custom navigation bar in B and C and I use "navigationController?.pushViewController" for the segues From A to B to C.
These are the codes.
Custom navigation bar:
class CustomNavBar: UINavigationBar {
let navBarView: UIView = {
let view = UIView()
view.backgroundColor = .black
// Logo
let logo = UIImageView(image: <image-file>)
view.addSubview(logo)
logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
logo.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
return view
}()
public func setupNavBar() {
if let window = UIApplication.shared.keyWindow {
window.addSubview(navBarView)
navBarView.topAnchor.constraint(equalTo: window.topAnchor, constant: 0).isActive = true
navBarView.leftAnchor.constraint(equalTo: window.leftAnchor, constant: 0).isActive = true
navBarView.rightAnchor.constraint(equalTo: window.rightAnchor, constant: 0).isActive = true
}
}
}
And in the ViewControllers B and C I put these codes.
class NavController1: UIViewController {
let navBar = CustomNavBar()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navBar.setupNavBar()
}
override func viewWillDisappear(_ animated: Bool) {
loginHeader.headerBg.isHidden = true
loginHeader.backButton.isHidden = true
}
}
And for the segues, I used the code below with a UIButton action.
self.navigationController?.pushViewController(NavController1, animated: true)
It works with the segue from A to B but it doesn't work with B to C.
Can someone point out what the problem is? Thank you!
performSegue(withIdentifier: , sender: )NavController1was created (in both cases)? Did you debugged eitherself.navigationControllernot nil (it shouldn't to be it you got to controller B by pushing it, but... )