I had an extensive conversation with an Apple engineer about this failure.
Here are the two most likely causes:
You have an invalid restriction, for example, view1.left = view2.left + 20 , where view2 unexpectedly zero or has a factor of 0. Be sure to double (and triple) check your limitations to make sure they are true. Here are two examples of problematic restrictions:
You get an error in the internal layout mechanism of the main flag associated with the accumulated loss of precision with floating point. When you crashed, how you can know that this is the case is to search the log for large (large) exceptions in the console for a very small (almost zero) floating point number, for example:
<505:-7.45058e-08>*PWPlotLegendEntryView:0x600000582be0.Height{id: 34609} +
(Search for e- in the console output to find such small numbers.) This number ( -7.45058e-08 in this case) represents the coefficient at that particular point in time, while the internal engine allows restrictions. In this case, the number should be exactly 0, but due to the way the auto-linking mechanism performs calculations with floating-point numbers, it has become an extremely tiny negative number, which blows everything up. If you can find such a quantity in the output, you know that you got into this error.
How can you get around this problem?
Changing the order of adding (activating) constraints can lead to a change in the order of calculations in the internal engine, which as a result can lead to the disappearance of this problem, since mathematics is performed without any problematic loss of accuracy.
This issue occurs more often when you change the content compression resistance or prioritize content for views, so try commenting on any code that does this to see if it caused an error or reordered it will happen sooner or later in your installation code restriction.
More about my specific case:
I ran into this crash on iOS. The steps to reproduce it were very interesting:
- The view controller containing the table view was clicked on the screen (in the navigation controller).
- There should be enough cells in the table view so that they do not all correspond to the visible area, then it had to be scrolled to the last cell, and then adjusted a little (presumably, this caused reuse of the cells, which caused this problem).
- Then, when the view controller containing the table view was removed from the navigation stack immediately after the pop animation completed, the application crashes at the point where the view controller view was removed from the view hierarchy.
After a lot of trial and error, I was able to single out the problem of one specific thing: set the compression resistance of the content and hug the priorities for UIImageView in each of the cells of the table. In this case, the image representation is positioned using an automatic layout inside the cell, and in order to achieve the correct layout, the image representation should be precisely its internal size of the content (the size of its image).
This was problematic code:
Removing the above code and replacing it with 2 restrictions (with mandatory priority) in order to fix the width and height of the image, so that the image size reaches the same result, but avoids a crash. Here's the replacement code (using PureLayout ):
[self.imageView autoSetDimensionsToSize:self.imageView.image.size]
I also found that simply moving the problematic 4 lines to another place in my restriction installation code solved the problem, apparently because it significantly changed the order of the calculations to prevent the problematic loss of precision.