在 Swift 中显示父控制器的导航栏时,在另一个控制器上方显示具有透明视图的模态视图控制器

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

我想显示一个透明背景的模态视图控制器,里面有一个小视图,它将显示为一个警告。我想在导航堆栈中的另一个视图控制器上方显示相同的内容。我尝试以多种方式展示我的第二个控制器,例如嵌入导航控制器、来自导航控制器、第二个视图控制器本身等。但是其中任何一个都不给我与父控制器相同的导航栏。我尝试将其添加为子视图,但随后未调用我的文本字段委托方法。谁能帮我解决这个问题。添加一些我尝试过的解决方案,这些解决方案是我从不同的堆栈溢出答案中获得的。 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)

非常感谢您...

我在这样的项目中做到了:

let controllerToPresent = UIViewController()
controllerToPresent.providesPresentationContextTransitionStyle = true
controllerToPresent.definesPresentationContext = true
controllerToPresent.modalPresentationStyle = .overCurrentContext
controllerToPresent.view.backgroundColor = UIColor.init(white: 0.4, alpha: 0.8)
navigationController.visibleViewController?.present(controllerToPresent, animated: true)
// OR
navigationController.present(controllerToPresent, animated: true)

希望对您有所帮助

我不知道如何呈现第二个控制器的视图。但最后我只将它添加为子视图。我的工作原理如下所示

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

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

并删除相同的赞

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

如果有任何其他解决方案有效,请告诉我。