That may be but Apple discourages “just handling them”. Their guidance is to fix the issues before your app gets to production. Now, maybe Xojo could figure out a way to only throw an exception in debug mode, but from what I’ve read, Apple may reject your app if you try to do that in production.
TBH, I don’t know what you’re expecting from an exception handler anyway. When the problem occurs, AutoLayout can’t figure out how to lay out the controls because it is Ambiguous (too few constraints) or Conflicted (too many). Maybe an untrappable exception? They’re typically called assertions…
I talked a little about this in my auto layout talk in Summer 2023, Because Xojo tried to simplify constraints for its developers, it glazes over two very important parts of constraints:
-
Priorities - by default, Xojo constraints are all “required” which means if you have too many or too few, you get a crash.
-
Hugging & Compression - not represented at all in xojo’s constraint system, but they control the priority of when a control is allowed to get larger or forced to get smaller when dealing with inequality constraints.
I also find that most users only use “equal to” for their constraints when the correct thing is “greater or equal” or “less or equal”. I find it usually affects people when they only consider a single device for layout purposes, thinking that the layout will just work.
Consider this scenario:
- Design Screen is 600pt wide
- Five canvases side by side, each 100pt wide
- Standard gap on left and right 20pt
- Dev decides on 15pt gaps between the canvases to make everything even.
100 x 5 = 500
500 + 20 + 20 = 540
540 + 15 x 4 = 600
Ok, so you’ve got a layout that works, ship it! But hold on a sec, there are 50 or so iPhone devices out there that can use your app, with 30 or so layout sizes. What happens if the screen width is 650, or worse 550? The constraints we made above make those layouts impossible if all of the constraints are required.
TLDR; if you’re going to use required “equals” constraints for everything, you should never have more than two per control, per direction:
- Left & Width or Top & Height
- Width & Right or Height & Bottom
- Right & Left or Top & Bottom
If you’re willing to use priorities, you can have more but you should not include any “required” constraints in that direction. You can also use inequalities as long as they don’t conflict. For instance don’t make width >= 30 and width <= 20 at the same time.
If you want better control over your constraints, check out my constraint project on GitHub.