来自不同视图控制器的 Swift 访问约束发现为零 [英] Swift Access Constraint from different View controller found nil

查看:24
本文介绍了来自不同视图控制器的 Swift 访问约束发现为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在解开可选值时发现 nil

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

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

在我的初始控制器上

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

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

尝试向该类添加两个变量

try to add two variables to that class

  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

and in viewDidLayoutSubviews

将变量应用于约束

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

      }
  }

这篇关于来自不同视图控制器的 Swift 访问约束发现为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆