2

having a bit of a problem here, so I was trying to cal a function of which it will change the height constraint constant, but it gives me an error :

"NSLayoutConstraint: Found nil while unwrapping optional Value"

Another catch is I can change the constant from the ViewDidLoad() function without any errors, while calling the function from another controller gives me the error.

I tried to clean the project, delete the outlet and re-outlet it again. No luck.

Hope you can help me, thanks!

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

}

Here's the storyboard Storyboard

R.DelaCruz
  • 78
  • 9
  • 1
    Where the anchor declared? and where all are you changing it? – Rakesha Shastri Aug 02 '18 at 10:37
  • 1
    Im not sure if I get your question correctly but anyway, The constraint outlet is declared on the **MainController**, if I change the constraint constant through the viewDidLoad of the **MainController** it does not throw any error, however if I call the function from the **MainController** from the **ChildController** it throws the error. – R.DelaCruz Aug 02 '18 at 10:40
  • Are you sure the outlet is correct? Because the outlet should be weak var Like this:- @IBOutlet weak var bottomContainerHeight: NSLayoutConstraint! – Anuraj Aug 02 '18 at 12:46
  • @Anuraj I changed it to weak, still have the problem. – R.DelaCruz Aug 02 '18 at 12:50
  • Create a fresh one after deleting this. – Anuraj Aug 02 '18 at 12:51
  • @Anuraj No luck, I wonder why it's not changing, I posted a link of the storyboard image. – R.DelaCruz Aug 02 '18 at 13:24
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Cristik Aug 02 '18 at 15:58
  • The outlet hasn't been loaded when you set the value, which means that you are setting the value to something that doesn't exist. – Joakim Sjöstedt Aug 11 '20 at 07:26

1 Answers1

1

Since you instantiate the VC , then the outlet is nil until view loads

@IBOutlet  var bottomContainerHeight: NSLayoutConstraint!

you need to do this

var bottomContainerCon: CGFloat = 0

then assign the value inside

viewDidLoad / viewDidLayoutSubviews like this

bottomContainerHeight.constant = bottomContainerCon
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87