Hello I'm trying to access my constraint from my second view controller to my initial view controller, but it always give an error, Im accessing my constraint from a pageviewontroller class, to automatically adjust the height of my second page.
found nil while unwrapping optional value
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController;
vc.bottomContainerHeight.constant = CGFloat(50)
vc.containerHeight.constant = CGFloat(30)
UIView.animate(withDuration: 0.5, animations: {
vc.view.layoutIfNeeded()
})
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
Sh_Khan Answer worked, but when I want to get back to the first page of the pageviewcontroller it doesn't get back to it's original size. Here's what i've tried
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
let pageContentViewController = pageViewController.viewControllers![0]
var page = orderedViewControllers.index(of: pageContentViewController)!
if(page == 1) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController
NeedDetailsController.status.viewstat = "true"
NeedDetailsController.status.bottomContainerHeightCon = CGFloat(50)
NeedDetailsController.status.containerHeightCon = CGFloat(30)
vc.view.layoutIfNeeded()
} else if(page == 0) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController;
NeedDetailsController.status.viewstat = "false"
NeedDetailsController.status.bottomContainerHeightCon = CGFloat(50)
NeedDetailsController.status.containerHeightCon = CGFloat(30)
vc.view.layoutSubviews()
}
}
and on my initial controller
override func viewDidLayoutSubviews() {
print("statusx: \(NeedDetailsController.status.viewstat)")
if(NeedDetailsController.status.viewstat == "true"){
print("statusy: \(NeedDetailsController.status.viewstat)")
NeedDetailsController.status.viewstat = "false"
self.bottomContainerHeight.constant = 0
self.containerHeight.constant = 0
UIView.animate(withDuration: 1) {
self.view.layoutIfNeeded()
}
} else if(NeedDetailsController.status.viewstat == "false") {
print("statusz: \(NeedDetailsController.status.viewstat)")
setupview()
NeedDetailsController.status.viewstat = "old"
self.bottomContainerHeight.constant = 50
self.containerHeight.constant = 30
UIView.animate(withDuration: 1) {
self.view.layoutIfNeeded()
}
}
}
You can't access a property whose view controller is not loaded
try to add two variables to that class
set them like this
and in viewDidLayoutSubviews
apply the variables to the constraints