func showCustomViewController(vc : UIViewController,dummy : UIView,fromView1:UIViewController){
datePickerShown = true
let fromView = fromView1.navigationController?.view
var navTopConstraint : NSLayoutConstraint!
vc.view.translatesAutoresizingMaskIntoConstraints=false
self.navigationController?.addChildViewController(vc)
dummy.addSubview(vc.view)
vc.didMove(toParentViewController: self.navigationController)
dummy.addConstraint(NSLayoutConstraint(item: vc.view, attribute: .top, relatedBy: .equal, toItem: dummy, attribute: .top, multiplier: 1, constant: 0))
dummy.addConstraint(NSLayoutConstraint(item: vc.view, attribute: .left, relatedBy: .equal, toItem: dummy, attribute: .left, multiplier: 1, constant: 0))
dummy.addConstraint(NSLayoutConstraint(item: vc.view, attribute: .width, relatedBy: .equal, toItem: dummy, attribute: .width, multiplier: 1, constant: 0))
dummy.addConstraint(NSLayoutConstraint(item: vc.view, attribute: .height, relatedBy: .equal, toItem: dummy, attribute: .height, multiplier: 1, constant: 0))
dummy.backgroundColor=UIColor.clear
if dummy.superview == nil{
fromView?.addSubview(dummy)
}
for constraints in dummy.constraints{
if constraints.firstAttribute == .top{
navTopConstraint = constraints
}
}
fromView?.addConstraint(NSLayoutConstraint(item: dummy, attribute: .width, relatedBy: .equal, toItem: fromView, attribute: .width, multiplier: 1, constant:0 ))
fromView?.addConstraint(NSLayoutConstraint(item: dummy, attribute: .top, relatedBy: .equal, toItem: fromView, attribute: .top, multiplier: 1, constant: -60))
fromView?.addConstraint(NSLayoutConstraint(item: dummy, attribute: .bottom, relatedBy: .equal, toItem: fromView, attribute: .bottom, multiplier: 1, constant: 0))
fromView?.addConstraint(NSLayoutConstraint(item: dummy, attribute: .left, relatedBy: .equal, toItem: fromView, attribute: .left, multiplier: 1, constant: 0))
navTopConstraint.constant = (fromView?.frame.maxY)!
fromView?.layoutIfNeeded()
UIView.animate(withDuration: 0.33, delay: 0, options:.curveEaseInOut, animations: {
dummy.backgroundColor=UIColor.init(white: 0.0, alpha: 0.4)
navTopConstraint.constant=0
fromView?.layoutIfNeeded()
}, completion: nil)
}
虚拟视图是添加到当前导航控制器视图的uiview。 vc是要添加到导航控制器视图的viewcontroller。 在vc viewcontroller中,调用viewdidload。 但是viewdidappear和viewwillappear永远不会被调用。为什么会这样?以及如何调用它。谢谢提前
答案 0 :(得分:2)
根据Apple's documentation添加视图控制器和视图的顺序是正确的,但是,有时在添加视图之前添加视图控制器时可能存在错误(可能是某种竞争条件)。
在将控制器添加为子视图控制器
之前,请先尝试添加视图dummy.addSubview(vc.view)
self.navigationController?.addChildViewController(vc)
您也可以尝试将addChildViewController()
方法包装在defer
块中。