Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa Autolayout - Why can't I delete or modify the (purple) width constraint on a Text Field?

I'm building a simple application using autolayout, and I've run into a strange situation. I place a Text Field in an empty part of a large open view so it's not affected by anything but the super view, but when I try to modify the "Width" constraint to be >= instead of ==, it creates a new constraint and refuses to modify the old one. I can't delete it, or change any of its attributes, because it just creates a new one.

Here is a comparison of the two constraints, the purple one being the stubborn one, and the blue one being the newly created one.

Constraint comparison

Why is the purple rounded one not modifiable?

like image 962
Chris Cooper Avatar asked Jan 02 '12 19:01

Chris Cooper


People also ask

How do I delete a constraint in XCode?

Open Utilities panel on XCode and select Size Inspector. Here list of all constraints are listed for selected view in storyboard. Now, select constraint(s) you want to delete. Now hit Backspace key on keyboard and selected constraints are deleted.

How do I change constraints in XCode?

Some editing is also possible directly from the Size inspector. Clicking the Edit button in any of the constraints brings up a popover where you can change the constraint's relationship, constant, priority, or multiplier.

What Colour do Autolayout constraints appear within interface builder if they conflict with each other?

In case of Autolayout constraints when you get these this means, Yellow - Your constraints may change in runtime, or can affect your layout, its shows due lack of all proper constraints that supports others, but can run without it.

How do constraints work in IOS?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.


4 Answers

I have worked around the presence of undeletable-but-unwanted constraints in IB by setting their priority to 1. Doesn't seem like the Right Thing to do, but sometimes I'm not smart enough to be a Cocoa developer.

like image 118
Kristopher Johnson Avatar answered Oct 13 '22 07:10

Kristopher Johnson


My problem had to do with with fact that there weren't enough other constraints added that the width would ever be forced to change. When I added more other constraints (such as leading and trailing space), I was then able to alter the purple constraint (in fact, it disappeared and I had to add my own).

It seems strange that you cannot add your own constraints unless there is a possibility of them being broken, but I guess that's the way it's been integrated into IB in some cases.

like image 11
Chris Cooper Avatar answered Oct 13 '22 07:10

Chris Cooper


Lowering the priority of the purple constraints will also make them editable.

like image 6
Jon Deokule Avatar answered Oct 13 '22 05:10

Jon Deokule


I had a similar scenario, where there were two multiline labels. Based on the content size, both should resize.

Two multiline labels

When the first label resized, it was overwriting the second label because the second one had a Vertical Space constraint( "Top Space to SuperView = 40". it's a system default constraint - purple colour) which I was not able to delete/modify.

If I tried to modify it as "Top Space to SuperView >= 40", it'd be changed to a user constraint( blue colour) and a new purple constraint "Top Space to SuperView = 40" would be created automatically.

I guess this could be the reason:

When I tried to change the constraint to "Top Space to SuperView >= 40", the label's default position is undefined : >= doesn't specify a default position. It specifies only a 'range of positions'. Then I added a new constraint by selecting both the labels together and setting the space between them as a constant. adding constraint between two labels

Now, since the first label had a definite position (vertical space = 15) from the top border and the second label was 5 points below the first one, the second label got a vertical position defined. I was able to delete the purple vertical space constraint.

Now, if I remove the constraint between the two labels, the second one will no more have the defined position and system will automatically create a purple constraint for the label.

like image 2
Joe M Avatar answered Oct 13 '22 07:10

Joe M