I have first view controller with navigation bar. Next I go to second view controller with navigation bar. And next I want to go to third view controller with navigation bar and overCurrentContext
options. And in third view controller I want to my back button return to first view controller. I use this code to do it:
In second vc:
let detailViewController = ThirdVC()
self.navigationController?.modalPresentationStyle = .overFullScreen
self.navigationController?.pushViewController(detailViewController, animated: false)
In third vc for back button:
self.navigationController?.popToRootViewController(animated: false)
But I can't see second vc as a background of third vc. .overCurrentContext
doesn't work. I see gray background. How to fix it?
.overFullScreen is a modalPresentationStyle, so It only work when you "present" the view modally. So it won't work with push, which push the ViewController into the navigation's stack.
In your case, if you really want to present the thirdVC modally and then pop to the rootViewController from secondVC as the secondVC has been push to the navigation's stack. You can achieve that by using delegate pattern to notify the secondVC to pop to re rootVC.
Another way is using closure as below:
In your thirdVC, declare a closure:
var onPopToRoot:(()-> Void)?
then in your pop function, for example:
@objc private func popToRoot() {
onPopToRoot?()
dismiss(animated: true)
}
In your secondVC:
@objc private func goThird() {
let detailViewController = ThirdVC()
self.navigationController?.modalPresentationStyle = .overFullScreen
detailViewController.onPopToRoot = {[weak self] in
self?.navigationController?.popToRootViewController(animated: true)
}
self.present(detailViewController, animated: true, completion: nil)
}