1

I have several UIViews in my ViewController that are initially hidden. They become unhidden once I get a successful response from Firebase. These initially hidden views have constraints (from Storyboard) that I am modifying in viewDidLayoutSubviews() based on device size.

Up until iOS 11.0, this worked perfect. Perfect = The views remained hidden. Once Firebase returned a value, the views became unhidden and were the correct size based on the constraints I have set in viewDidLayoutSubviews().

From iOS 11.0.1 and up, I'm now crashing in viewDidLayoutSubviews().

fatal error: unexpectedly found nil while unwrapping an Optional value

Here is the code in my viewDidLayoutSubviews() that's causing the crash in only iOS 11.0.1 and up.

override func viewDidLayoutSubviews() {
    if UIScreen.main.bounds.size.height == 736 { // iPhone 8+
        welcomeViewWidthConstant.constant = 160.0
        welcomeViewHeightConstant.constant = 160.0
    }
}

welcomeViewWidthConstant and welcomeViewHeightConstant are the height and width constraints for the view that is not visible until the Firebase response.

And again, the above was working perfect in iOS 11.0

How can I set the constraints (based on device size) in iOS 11.0.1 and above?

Joe
  • 3,772
  • 3
  • 33
  • 64
  • can you try the current code on iOS 10.x. You might be searching the solution in wrong direction. – Mohammad Sadiq Oct 26 '17 at 18:44
  • I would change the NSLayoutConstraints to optionals and unwrap them in the function... Also you should call super.viewDidLayoutSubviews... – Dominik Bucher Oct 26 '17 at 19:51
  • 1
    Which line exactly is causing the error? Clearly some implicitly unwrapped variable is `nil`. – rmaddy Oct 26 '17 at 20:00
  • @rmaddy - you're correct! I didn't realize that the constraints connections were disconnected in storyboard. So my code to update the constraint constants was referencing a nil variable. – Joe Oct 26 '17 at 20:10
  • By the way, don’t forget to call `super` when you override `viewDidLayoutSubviews`. – Rob Oct 26 '17 at 21:24
  • @Rob Thanks for the heads up! super is added :) – Joe Oct 27 '17 at 01:48

2 Answers2

0

I am assuming that welcomeViewWidthConstant is an outlet that you declared with a ! (implicitly unwrapped).

If so, I would look at the stack trace that this is being called in -- is it possible the view is not loaded?

Add

guard let self.isViewLoaded else { return }

to the top of the function

But, I also think it's weird that you would change a constraint in didLayout. I would expect that you would call setNeedsUpdateConstraints on the return from Firebase (probably moving to main queue) and then override updateConstraints to do the update.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

Folks, thank you for lending a hand! I actually found the culprit... The connection to the constraint was disconnected within the storyboard. So the @IBOutlet for welcomeViewWidthConstant and welcomeViewHeightConstant was in fact nil. What was throwing me off was my device isn't an iPhone 8+ size, so it slipped through the cracks.

Joe
  • 3,772
  • 3
  • 33
  • 64