Swift Access Constraint from different View contro

2019-06-14 18:34发布

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

标签: ios swift swift3
1条回答
时光不老,我们不散
2楼-- · 2019-06-14 18:50

You can't access a property whose view controller is not loaded

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

try to add two variables to that class

  var bottomContainerHeightCon:CGFloat!

  var containerHeightCon:CGFloat!

set them like this

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

and in viewDidLayoutSubviews

apply the variables to the constraints

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

      }
  }
查看更多
登录 后发表回答