Can't add constraints in xib - ios

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.

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.

You should set layout property of your view to Inferred (Constraints)

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.

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:

Related

add constraint to an empty scrollview on xib or storyboard ambiguity

I added an empty scrollView on a view,set leading/top/trailing/bottom,then the frame is accurate,but when set leading/top/trailing/height,xib shows ambiguity error! if i set constraint by code ,it will be ok,why???is a bug of xib?
Storyboard / Interface Builder is telling you that the scrollView has ambiguous content size because it has no contents.
You can either:
ignore this "error" indicated in IB, or
you can add a UIView as a subview of the scrollView (setting proper constraints), then at run-time either remove that view or add your UI elements to it (instead of adding to the scrollView directly).
As a side note... if your entire XIB file contains just a scroll view, don't do that. Just use a scroll view. If this is simply your first step, and you are going to be adding UI elements to the scroll view in the XIB as you proceed with your design process, then you will see the error go away as soon as you add elements and setup the proper constraints.
You need to add a view to your scrollView and link them with leading/top/trailing/bottom constraints. Also you got to set height/width constraints to just added view to remove ambiguity.

Set AutoLayout width constraint on top-level UIView of custom XIB from Interface Builder or code?

How do you set a width constraint on the top-level view of a custom XIB?
This would be Content View in the image below.
Clicking the Add New Constraints button doesn't let you specify a width. The option is disabled (along with other options).
Is this possible from Interface Builder? If not, is this possible from code? If not, that might explain some layout issues ...
Auto-layout sets the .translatesAutoresizingMaskIntoConstraints to true on the top-level view loaded from a custom XIB.
You want to set the .translatesAutoresizingMaskIntoConstraints to false via code when you load and use that view.
Is this possible from Interface Builder?
No. If you have a fixed size to give the view, then set its Size metric to Freeform and give it that size, directly.
If not, is this possible from code?
Yes, of course. You must have, somewhere, some code that loads the nib, obtains the view, and puts the view into the interface. At that point it is up to you to size and position it, and you can certainly use autolayout to do so, though no law requires that you do it that way; you could just give it a frame and leave things at that, or if the size coming from the nib is right, then you can leave it alone and just position the view with its frame origin or center.

Set constraint for UIView that is inside UITableView

This is my view hierarchy:
I want to set height constraint for the View, but it doesn't allow me to set it in the storyboard, and when I set it programatically I got a runtime error (nil)
I went through your case in one of my project, which I needed to embed a view in a static table view. Yes, what different here is that IT IS STATIC, and you can create as many sections as you want.
What I did is really simple, you just need to adjust the view as you wish, then uncheck the axis that you don't want it to be auto layout.
For example, I wanted my view to fit with 44 as its height, and didn't want it to scale bottom down way. So I configed it as the picture bellow.
Hope it helps.

Xcode 8 : Can i use Constraints and autosizing both in Single View?

There is autosizing option available until you give any constraints to UI Component.
So can I use both for my ViewController?
You could, but you shouldn't
You can use constraints on some views, and autosizing on others, but be careful not to mix them on the same view, as that will cause issues (the autosizing information will be lost).
iOS takes care of autosizing views by creating constraints that convey the information of the autosizing to the constraints engine. This behaviour can be enabled or disabled by the aptly named translatesAutoresizingMaskIntoConstraints property.
You could try to add constraints to a view and still have it autoresize with the old behaviour by setting this value to true, but I suggest you use constraints for every view, as it can do everything the autosizing can, and much more.
Yes, you can (use a mixture of constraints and autoresizing on subviews within a single view) within Xcode 8. See:
02:38 https://developer.apple.com/videos/play/wwdc2016/236/
With Xcode 8, the translatesAutoresizingMaskIntoConstraints property for each view is automatically maintained by Interface Builder:
By default, when a child view is first added, the value of the property is true.
When the first constraint is added to the child view, the value is automatically set to false and the Size Inspector pane changes to the Constraints view.
When the last constraint is removed from the child view, the value of the property is automatically reverted to true, and the Size inspector reverts to the Autoresizing view.
If you create a view programatically, Xcode will generate constraints to satisfy autoresizing itself (translatesAutoresizingMaskIntoConstraints is set to true). However if you create a view in IB, Apple says:
If you add views in Interface Builder, the
system automatically sets this property to false.
To reflect that, any view added into IB before Xcode 8 has that autoresizing option hidded (or at least I did not see it for a while there).
But here is the thing:
Since Xcode 8, the option is visible (translatesAutoresizingMaskIntoConstraints is set to true) until you add any constraint, so the Apple's text above is not actually correct I guess.
So in a single view you cannot satisfy/use both. view(autoresizing and constraints)
In subviews you can use different for each one, but it will be a mess and I cannot imagine reasonable usage. view->subview(constraints) and subview(autoresizing)
That also means that you can use e.g. view(autoresizing)->subview(constraints)

Keep autolayout contraints after changing superview in the storyboard

I have a UIView in the storyboard where all the constraints are defined. Now I have to change it so that all the subviews are inside a new scroll view. I drag a scroll view into the view, adjust the size and then add all the subviews into the scroll view. All the autolayout constraints are now out of place and the best solution I find is to delete the constraints and create them again, but this is very time consuming. Is there a better way to do this?
Unfortunately, there is no easy way to transfer the constraints. You can take a look at this article that shows how to edit your storyboard in a text editor.
One thing that can at least help a little bit is the "Embed In" functionality. This will wrap a container around your selected child views and at least save you the time of repositioning everything.
Editor > Embed In > Scroll View

Resources