Xcode 11 Add "Equal Width to Superview" Constraint?

When I drag to make a constraint from a view to its superview in Interface builder, I no longer see the "Equal widths" in the list of constraints to choose.


If I click the little "Add New Constraint" button, I do have the ability to make a "width" constraint and specify a static value, but I don't see how to make equal width relative to the superview?

Replies

Yes, it has disappeared. I cannot find a serious reason for that.



What you can do is add a leading and trailing equal to 0.

But the ratio option is not possible.

I see that. So the setting an equal width/height constraint is no longer supported?


Also adding constraint to the superview actually adds leading/trailing constrain to the safe area. Have to manually change the second item to the superview in the identity inspector. Not sure why. If I'm making a constraint from a scroll view, I want to use the superview, since the scoll view will adjust its constent inset to account for the safe area.....

Yes for some reason I had to make a spacer view constrained to the superview, then make my other view proportional to that. Couldn’t seem to make it proportional to the superview directly without the middle man.

The same happens to me. I think this is quite unacceptable. I rely heavily on this disappearing feature. I noticed that with auto layout it shows up but it is greyed out.

Have you reported this bug to Apple? I am having the same problem.

I didn't try it. But it is still probably possible to do it all in code (not very user friendly).

You can constrain the width to the Safe Area width, but not sure if thats applicable to your specific layout. Not sure why they would disable this functionality, seems like a bug to me. I think I will file a report later as I just encountered this as well and I do this often to create proportional widths.

You can make it equal to Superview by:

1. Drag and make width equal to Safearea

2. Go to constraints in interface builder and find that width constraint

3. In properties of particullar constraint change Safearea to Superview

4. Done!

A tweak would be to

1. First do "Equal Widths " to any other UIView element .

2. Go to Attributes inspector of the equal width contraint .

3. Replace the Second Item with Superview.Width.

I found a better solution to this. You can easily add 1 line to the storyboard/XiB file. I was lucky to have a previously existing constraint that I could use as a reference, so I'm sharing it with those out there that don't have that luck.

This is the line I added:
<constraint firstItem="w8A-AW-Z0t" firstAttribute="width" secondItem="iGR-Zz-5CD" secondAttribute="width" multiplier="1/5" id="Mwe-3A-te7"/>

Where:
  • "w8A-AW-Z0t" is the child view,

  • "width" is self-explanatory,

  • "iGR-Zz-5CD" is the parent view,

  • "1/5" is whatever ratio you want to use --on this example, the child's width is a fifth of the parent's,

  • "Mwe-3A-te7 is the id of the constraint, you put there any string you wanna come up with that follows the similar pattern "{3 chars}-{3 chars}-{3 chars}". And, of course, it must not be already used by any other existing element (Cmd+F).

Just open up the storyboard/XiB file as source code. The above line must be added to the parent view. Assuming the parent has other constraints, you'll be able to see pretty easily where to add it. The order in the constraints array doesn't matter, it will work as long as you place it in the right section.

Pretty simple. Hope it helps.

Good luck,
LucasV