Cocoa Autolayout - Why can't I delete or modif

2020-07-02 10:39发布

问题:

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.

Why is the purple rounded one not modifiable?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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

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.

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.



回答5:

When you right-click on the constraint, select "Promote to user constraint". Next time you click on constraint, you will be able to delete it as now it is in the hands of the user/developer.