Search code examples
swiftuinavigationcontrollermodalviewcontrollerpresentmodalviewcontroller

Displaying a modal view controller with transparent view above another controller while displaying navigation bar of the parent controller in Swift


I would like to display a modal view controller with transparent background which has a small view inside, which will appear as an alert. I want to display the same above another view controller which is in a navigation stack. I tried to present my second controller in many ways like getting embedded in navigation controller, from navigation controller , second view controller itself etc . But any of these doesn't give me the same navigation bar as the parent controller. I tried adding it as a subview, but then my textfield delegate methods are not getting called. Could anyone please help me with a solution for this. Adding some of the solutions that I tried which I got from different stack overflow answers.. 1.

guard let alertController = /* my controller */ else { return }
let navController = UINavigationController(rootViewController: alertController)
alertController.delegate = self
alertController.valueText = "Value"
alertController.userPhoneNumber = updatedUserPhoneNumber
navController.modalTransitionStyle = .crossDissolve
navController.modalPresentationStyle = .currentContext
present(navController, animated: false, completion: nil)

2.

guard let alertController = /* my controller */ else { return }
alertController.delegate = self
alertController.valueText = "Value"
alertController.userPhoneNumber = updatedUserPhoneNumber
alertController.modalTransitionStyle = .crossDissolve
alertController.modalPresentationStyle = .currentContext
navigationController.present(alertController, animated: false, completion: nil)

3.

guard let alertController = /* my controller */ else { return }
let navController = UINavigationController(rootViewController: alertController)
alertController.delegate = self
alertController.valueText = "Value"
alertController.userPhoneNumber = updatedUserPhoneNumber
navController.modalTransitionStyle = .crossDissolve
navController.modalPresentationStyle = .currentContext
navigationController.present(navController, animated: false, completion: nil)

4.

guard let alertController = /* my controller */ else { return }
alertController.delegate = self
alertController.valueText = "Value"
alertController.userPhoneNumber = updatedUserPhoneNumber
alertController.modalTransitionStyle = .crossDissolve
alertController.modalPresentationStyle = .currentContext
present(alertController, animated: false, completion: nil)

Thank you very much in advance...


Solution

  • I didn't get an idea on how I can present the second controller's view. But finally I added it as a subview only. And how I got it worked is like below

    https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

    addChild(alertController) view.addSubview(alertController.view) alertController.didMove(toParent: self)

    and removing the same like

    willMove(toParent: nil) view.removeFromSuperview() removeFromParent()

    Please advise me if any other solution works.