Andy in Boulder
Andy in Boulder

Reputation: 471

"Add New Constraints" checkboxes and fields are disabled

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.
enter image description here 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

Answers (6)

Marcy
Marcy

Reputation: 6019

Xcode 12 and later versions

For UI elements where adding constraints are disabled, check Layout in the Size Inspector:

enter image description here

If Layout is set to Autoresizing Mask change to Inferred.

enter image description here or enter image description here

Inferred(Constraints) - The UI element already has one or more constraints. Inferred(Autoresizing Mask) - The UI element currently has no constraints.

Upvotes: 54

Saif
Saif

Reputation: 2968

Steps to enable autolayouts:

  1. Select any UI element, in storyboard.
  2. Click on Show the Size Inspector.
  3. Click on dropdown beside Layout option, select Automatic from dropdown.

enter image description here

Upvotes: 8

Neha
Neha

Reputation: 742

Xcode 12 Swift 5

  • Select the UI Control and click on Show Size Inspector window enter image description here
  • Change Layout option to Inferred (Autoresizing Mask)enter image description here)

By default the Xcode 12 keep the setting of layout to Autoresizing Mask, If you want to apply the constraint used Inferred

Upvotes: 3

mary alexis solis
mary alexis solis

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

Krešimir Prcela
Krešimir Prcela

Reputation: 4281

Trick to enable constraints on the root view:

  • Inside the xib, drag a new view that will be sibling to the initial root view.
  • Move the initial root view inside the sibling view. It will be able to have own constraints.
  • Move the initial view back to be a root.
  • Delete the empty sibling view.

Upvotes: 2

Andy in Boulder
Andy in Boulder

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

Related Questions