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

时间:2018-01-22 06:41:51

标签: ios swift swift3

您好我试图从我的第二个视图控制器访问我的约束到我的初始视图控制器,但它总是给出一个错误,我从一个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()
        }   
    }   
}

1 个答案:

答案 0 :(得分:2)

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

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

      }
  }