Govind Rakholiya
Govind Rakholiya

Reputation: 467

How To Present New Controller on Already Presented Controller

In my App i am Capturing image by custom camera. So at the time of capturing image i am presenting controller so it is showing camera in center and its looks like capturing image. Here its code for that

    let layerVC = cameraLayerViewController(nibName: "cameraLayerViewController", bundle: nil)
            layerVC.modalPresentationStyle = .custom

            Global.appDelegate.window?.rootViewController?.presentedViewController?.present(layerVC, animated: false, completion: nil)

            DispatchQueue.main.asyncAfter(deadline: .now() + 0.4, execute: {

                layerVC.dismiss(animated: false, completion: nil)
}

but when changing camera mode i am presenting another controller on same controller

if (UserDefaults.standard.value(forKey: Global.g_UserDefaultKey.isFrontCamera) as? Bool == true)
    {
        UserDefaults.standard.set(false, forKey: Global.g_UserDefaultKey.isFrontCamera)
        let controller : CameraVC = CameraVC(nibName: "CameraVC", bundle: nil)

        self.present(controller, animated: false, completion: nil)
    }
    else
    {
        UserDefaults.standard.set(true, forKey: Global.g_UserDefaultKey.isFrontCamera)
        let controller : CameraVC = CameraVC(nibName: "CameraVC", bundle: nil)
        self.present(controller, animated: false, completion: nil)
    }

now when on this controller i am trying to present any controller its not working.

    let layerVC = cameraLayerViewController(nibName: "cameraLayerViewController", bundle: nil)
            layerVC.modalPresentationStyle = .custom

            Global.appDelegate.window?.rootViewController?.presentedViewController?.present(layerVC, animated: false, completion: nil)

            DispatchQueue.main.asyncAfter(deadline: .now() + 0.4, execute: {

                layerVC.dismiss(animated: false, completion: nil)
}

so let me know how can i present controller on already presented controller.

Upvotes: 2

Views: 2137

Answers (1)

slushy
slushy

Reputation: 12385

To present the first view controller from the root:

let root = UIApplication.shared.keyWindow!.rootViewController!
let firstPresented = FirstViewController()
firstPresented.transitioningDelegate = yourPresentationDelegate
firstPresented.modalPresentationStyle = .custom
root.present(firstPresented, animated: false, completion: nil)

To present the second view controller, you can't present from the root again since it's already presenting, so you must present from what is currently presented (which is self):

let secondPresented = SecondViewController()
secondPresented.transitioningDelegate = yourPresentationDelegate
secondPresented.modalPresentationStyle = .custom
self.present(secondPresented, animated: false, completion: nil)

Upvotes: 2

Related Questions