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.
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.
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.
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.
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.
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:
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:
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.
The new view should have the autoresizing mask with everything checked.
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.