admin管理员组

文章数量:1336632

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?

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?

Share Improve this question edited Nov 19, 2024 at 19:49 Alex Smith asked Nov 19, 2024 at 16:18 Alex SmithAlex Smith 291 silver badge10 bronze badges 10
  • @AlexSmith Your original question was better than your edit. Since you want a clear background on ThirdVC, you have to present it, not push it. So now your issue is how to pop SecondVC when you dismiss ThirdVC. – HangarRash Commented Nov 19, 2024 at 19:55
  • @DonMag The OP needs to present, not push, ThirdVC since the OP wants ThirdVC to have a clear background. modalPresentationStyle is not used when a VC is pushed, only when presented. – HangarRash Commented Nov 19, 2024 at 19:57
  • @HangarRash Yes. That's the question. When I return on button click I need to dismiss third vc and second vc. – Alex Smith Commented Nov 19, 2024 at 19:58
  • @HangarRash I think original question was better than your edit. But DonMag and DuncanC didn't understand the question. So I simplified it. – Alex Smith Commented Nov 19, 2024 at 20:02
  • @AlexSmith Where is the dismiss button? In the SecondVC or the ThirdVC? – HangarRash Commented Nov 19, 2024 at 20:04
 |  Show 5 more comments

1 Answer 1

Reset to default 1
  • .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)
    }

本文标签: iosHow to move between view controllersStack Overflow