25

I have a xib but I can't add constraints. When I control click and drag I can't do anything also the buttons at the bottom I can't do anything with it.

I'm using xCode 11.

enter image description here

user1007522
  • 7,858
  • 17
  • 69
  • 113

7 Answers7

43

Check if the layout option is set to automatic or not for the views/buttons etc for which you trying to add constraints. Attaching a screenshot for reference, hope this helps. enter image description here

Sumit
  • 874
  • 9
  • 10
10

Please also pay attention to the Layout parameter in the Size Inspector. There are 2 kinds of values for each component in the xib: Inferred (Constraints) and Autosizing Mask.

If Inferred (Constraints) is selected, the view is added to the parent view without the autosizing constrains. It means no matter what autosizing you set for this view, it has no affect when added to the parent view. In fact, it is the same as "translatesAutoresizingMaskIntoConstraints" property set to false.

As the contrast, if Autosizing Mask is selected, the view's autosizing parameters you defined in the view will be applied when added to the parent view. It should be selected if you are using autosizing in xib.

screenshot the Layout parameter

Jay Lee
  • 111
  • 1
  • 4
  • 3
    I opened an old project and found a different option labelled "Inferred (Autoresizing Mask". This option will turn into "Inferred (Constraints)" once you add a constraint. See here: https://developer.apple.com/forums/thread/669586 – ThomasW Aug 03 '22 at 09:21
  • Just for the record apple... i absolutely hate this – Magoo Aug 14 '23 at 04:35
5

I had this issue and I just figured out what caused it.

When I added a new button to my view controller XIB, I added the button to the open space next to the XIB and then dragged the button under the View in the Document Outline on the left. If you do this, for some reason, it doesn't let you Ctrl-drag to the view to add constraints.

If you add a new button and drag it into the view immediately, it should allow you to Ctrl-drag constraints into existence.

vgruiz
  • 51
  • 1
  • 2
2

Xcode is very confusing sometimes but and when I was trying to pick from Layout option in the UI, it didn't give me Constraints, and as instead my Inferred was the same as currently selected option Autoresizing Mask which caused the most confusion:

enter image description here

But if you actually be brave and go ahead, instead of Autoresizing Mask it will activate Constraints and you are able to setup constraints hereinafter:

enter image description here

Mando
  • 11,414
  • 17
  • 86
  • 167
1

Try to embed the views in a view using the Embed in button. Set the Layout attribute of the new view to Autoresizing Mask. Now you can use auto layout in the views inside the new view.

enter image description here

The new view should have the autoresizing mask with everything checked. enter image description here

rockdaswift
  • 9,613
  • 5
  • 40
  • 46
1

You should set layout property of your view to Inferred (Constraints) enter image description here

Oleg Kovtun
  • 362
  • 1
  • 3
0

For me on macos 10.15.6, XCode 12.4, this happens because I copied a NSViewController root view and tried to modify it as a child view. Seems like it also copies some hidden "can't be constrained" parameter.

So, just don't do what I did.

Starwave
  • 2,352
  • 2
  • 23
  • 30