0
votes

I am removing login view controller from navigation stack once user gets logged in. However, the navigation bar button items added by the login view controller still remains. How to completely remove a view controller from navigation stack?

static func removePreviousFromNavigationStack(_ navVC: UINavigationController) {
    var navArr = navVC.viewControllers
    for elem in navArr {
        if elem.isKind(of: LoginViewController.self) {
            if let vc = StateData.loginVC {
                vc.navigationItem.leftBarButtonItem = nil  // is not working as intended
                vc.navigationItem.rightBarButtonItem = nil
                vc.navigationItem.title = "Foo"
            }
            //elem.removeFromParent()
        }
    }
    navArr.remove(at: navArr.count - 2)
    navVC.viewControllers = navArr
}

Flow: HomeVC -> ApplyVC -> LoginVC -> FormVC

After logging in to FormVC, I call the remove method to remove LoginVC from the stack. This removes the VC, but the nav buttons remains. If I set the nav button to nil, the ApplyVC's leftButtonItem, back button, right button item, home button does not show. There is a transparent back button which when clicked, displays the nav bar buttons of ApplyVC as if the LoginVC got popped out of the view, but without any changes to the current view.

screens

1
I think the issue is with hiding/show custom and default navigation bar. can you please tell me what you have do in applyVc and here what is StateData?Nikunj Kumbhani
In LoginViewController's viewDidLoad(), I am setting StateData.loginVC = self. The ApplyVC has navbar items added. In login VC, the same is done. But after removing the login vc from stack, the apply vc navbar items are not displayed properly as shown in the screenshot.johndoe
First, are you sure the code inside if let vc = StateData.loginVC { ... } is running? That is, vc does equal StateData.loginVC? If so, it's not entirely clear what all you are doing, so try to put together a minimal reproducible example so folks can help you debug this.DonMag

1 Answers

3
votes

Try removing the LoginVC at the time of pushing FormVC instead of after the FormVC is visible.

I’ve created the same NavigationStack as yours.

1. Added a rightBarButton in HomeVC

class HomeVC: UIViewController {
    var rightBarItem: UIBarButtonItem = {
        return UIBarButtonItem(barButtonSystemItem: .bookmarks, target: nil, action: nil)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "HomeVC"
        self.navigationItem.rightBarButtonItem = rightBarItem
    }
}

class ApplyVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "ApplyVC"
    }
}

2. Added a rightBarItem in LoginVC

class LoginVC: UIViewController {
    var rightBarItem: UIBarButtonItem = {
        return UIBarButtonItem(barButtonSystemItem: .add, target: nil, action: nil)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "LoginVC"
        self.navigationItem.rightBarButtonItem = rightBarItem
    }

    @IBAction func onTapButton(_ sender: UIButton) {
        var controllers = self.navigationController?.viewControllers
        let formVC = self.storyboard?.instantiateViewController(withIdentifier: "FormVC") as! FormVC
        controllers?.removeAll(where: { $0 is LoginVC })
        controllers?.append(formVC)
        if let controllers = controllers {
            self.navigationController?.setViewControllers(controllers, animated: true)
        }
    }
}

In the above code, I’ve filtered the LoginVC and added FormVC from navigationController’s viewControllers array.

class FormVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "FormVC"
    }
}

enter image description here