Reputation: 73
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)
}
dummy view is the uiview added to the current navigation controller view. vc is the viewcontroller to be added to the navigation controller view. In vc viewcontroller, viewdidload is called. But viewdidappear and viewwillappear is never called.why is it so? and how to call the same.Thanks in advance
Upvotes: 1
Views: 462
Reputation: 223
According to Apple's documentation the order you added the view controller and view is correct, however, sometimes there seems to be a bug when you add the view controller before adding the view (possibly some sort of race condition).
Try adding the view first before adding the controller as child view controller
dummy.addSubview(vc.view)
self.navigationController?.addChildViewController(vc)
You could maybe also try to wrap the addChildViewController()
method inside a defer
block.
Upvotes: 2