我正在通过导航控制器编程添加一个视图控制器,因为我需要在显示控制器时访问导航栏。
我目前正在通过下面的方法来完成这项工作,但是我需要能够在用户点击dissive按钮时移除对视图控制器的引用。
let settingsController = storyboard?.instantiateViewController(withIdentifier: "SettingsController") as! SettingsController
settingsController.view.alpha = 0
settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height)
self.view.addSubview(settingsController.view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
print(self.view.frame.height)
settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height - 142)
settingsController.view.alpha = 1
}, completion: nil)
当当前的self.dispose或self.popViewController不工作时,是否有方法可以执行此操作?
谢谢
更新
我试图使用标准的perform segue(如下)来实现这个解决方案,因为我需要保持导航栏的活动性,所以我提供了modally选项。设置控制器高度小于图幅。这不起作用,所以我正在尝试上述方法。
self.definesPresentationContext = true
self.modalPresentationStyle = .overCurrentContext
self.modalTransitionStyle = .coverVertical
self.performSegue(withIdentifier: "SettingsSegue", sender: nil)
当我调用设置控制器并关闭时,会添加另一个设置控制器,从而产生多个设置控制器。这是我的问题。
最佳答案
变量设置控制器:设置控制器!
@IBAction func btnNext(_ sender: Any) {
settingsController = storyboard?.instantiateViewController(withIdentifier: "SettingsController") as! SettingsController
settingsController.view.alpha = 1
settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height)
self.view.addSubview(settingsController.view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
print(self.view.frame.height)
self.settingsController.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height - 142)
self.settingsController.view.alpha = 1
}, completion: nil)
}
@IBAction func btnDismiss(_ sender: Any) {
self.settingsController.view.removeFromSuperview()
}
关于ios - 快速删除对 View Controller 的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42804780/