Reputation: 471
I have a project I am upgrading from Xcode 4.6.3 to Xcode 6.1.1. I opened it in Xcode 6.1.1, and opened each .xib. The format of each .xib changed as expected. I want to attempt to use auto layout. Use Auto Layout is checked and so is Use Size Classes. I changed every appropriate object from Alignment Frame to be Alignment Rectangle.
For a while I couldn’t add any constraints. Then after some trying I could add a few. See the screenshot.
For the selected View, I cannot add a constraint. The Add New Constraints checkboxes and fields are disabled. Only Update Frames is available to be changed. I can’t add an alignment constraint either. Ctrl-drag a line off the view does not add a constraint either. Notice the View height of 411. If I select another object, then select the View again the height will change to be the height of the parent tab bar less. If I keep doing that the height gets smaller and smaller, then Xcode crashes. How can I add a constraint to this view?
The Tab Bar X, Y, Width, and Height are disabled. When I change the Height of the Assigned View Controller View, the Tab Bar Y changes to that value minus the Tab Bar Height of 49. When I select the View, again it’s Height is now 49 less. Select the Tab Bar, it’s Y is now 49 less. And so on. How do I set the dimensions of the Assigned View Controller View that will stick when I cannot add contraints?
Upvotes: 34
Views: 14740
Reputation: 6019
Xcode 12 and later versions
For UI elements where adding constraints are disabled, check Layout in the Size Inspector:
If Layout is set to Autoresizing Mask change to Inferred.
Inferred(Constraints) - The UI element already has one or more constraints. Inferred(Autoresizing Mask) - The UI element currently has no constraints.
Upvotes: 54
Reputation: 2968
Steps to enable autolayouts:
Show the Size Inspector
.Layout
option, select Automatic
from dropdown.Upvotes: 8
Reputation: 742
Xcode 12 Swift 5
By default the Xcode 12 keep the setting of layout to Autoresizing Mask, If you want to apply the constraint used Inferred
Upvotes: 3
Reputation: 571
I had the same issue. In my case, the view layout setting was set to Translate Mask Into Constraint
. I resolved it by changing it to Automatic
in Size Inspector
.
Upvotes: 57
Reputation: 4281
Trick to enable constraints on the root view:
Upvotes: 2
Reputation: 471
Zev has the answer. You can't add constraints directly to the top-level view in a view controller.
In regards to the height of the View decreasing when selecting it, I started over from the original .xib making small changes and taking notes. When I checked "Use Auto Layout" and "User Size Classes", got alert the document will no longer be compatible with Xcode 5. Window frame size went from 320 568 to 600 600. ibExternalTranslatesAutoresizingMaskIntoConstraints went from 1 to 0. Other changes apparent as well. Adding constraints to objects contained in View one at a time I am getting good results. The behavior of the View height decreasing when I select it is gone.
Upvotes: 2