NSLayoutConstraint:在展开可选值时发现nil

时间:2018-08-02 10:32:08

标签: swift xcode swift3 swift4

这里有一个问题,所以我试图校准一个函数,它将改变高度约束常数,但是它给了我一个错误:

“ NSLayoutConstraint:解开可选值时发现nil”

另一个问题是,我可以从 ViewDidLoad()函数更改常量而没有任何错误,而从另一个控制器调用该函数会给我带来错误。

我试图清洁项目,删除出口,然后再次重新出口。没有运气。

希望您能帮助我,谢谢!

MainController

var bottomContainerCon: CGFloat = 80
@IBOutlet  var bottomContainerHeight: NSLayoutConstraint!
   override func viewDidLoad() {
    super.viewDidLoad()
   bottomContainerHeight.constant = bottomContainerCon
   }

    func changeHeight() {
    bottomContainerHeight.constant = self.bottomContainerCon 
    self.view.layoutIfNeeded()
 }

PagerController

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController


    let currentIndex = pages.index(of: previousViewControllers.first!)!

    let nextIndex = abs((currentIndex + 1) % pages.count)


    if(nextIndex == 0) {
        vc.changeHeight(value:80)
    } else {
        vc.changeHeight(value:250)
    }


  override func viewDidLayoutSubviews() {
    self.bottomContainerHeight.constant = self.bottomContainerCon
    self.view.layoutIfNeeded()

}

这是情节提要Storyboard

1 个答案:

答案 0 :(得分:0)

自实例化VC以来,出口一直为零,直到视图加载

@IBOutlet  var bottomContainerHeight: NSLayoutConstraint!

您需要这样做

var bottomContainerCon: CGFloat = 0

然后在其中分配值

viewDidLoad / viewDidLayoutSubviews

bottomContainerHeight.constant = bottomContainerCon