来自不同View控制器的Swift Access Constraint发现为零

您好我正在尝试从我的第二个视图控制器访问我的约束到我的初始视图控制器,但它总是给出一个错误,我从一个pageviewontroller类访问我的约束,以自动调整我的第二页的高度。

在展开可选值时发现nil

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答案有效,但当我想回到pageviewcontroller的第一页时,它没有恢复到它的原始大小。 这是我尝试过的

  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() } } 

并在我的初始控制器上

 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() } } } 

您无法访问未加载视图控制器的属性

  vc.bottomContainerHeight.constant = CGFloat(50) vc.containerHeight.constant = CGFloat(30) 

尝试向该类添加两个变量

  var bottomContainerHeightCon:CGFloat! var containerHeightCon:CGFloat! 

像这样设置它们

  let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController; vc.bottomContainerHeightCon = CGFloat(50) vc.containerHeightCon = CGFloat(30) 

并在viewDidLayoutSubviews中

将变量应用于约束

  override func viewDidLayoutSubviews { if(once){ once = false self.bottomContainerHeight = bottomContainerHeightCon self.containerHeight = containerHeightCon self.view.layoutIfNeeded() } }