Lance
Lance

Reputation: 1171

Xcode 6.4 - Unable to set constraints on Views or ... anything

There are many guides out there that talk about auto-layout such as: http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1

Now constraints don't seem to be super complicated except that when it comes to trying to set them on a UIImageView within a ViewController, With "Use Auto-Layout" ticked, I can't for the life of me set any constraints as when I click any of the three buttons used to achieve this, all the options for setting constraints is greyed out.

I'm sure this is something simple... little help?

I've found something in reference materials: "Constraint options that require multiple elements are disabled if you have only a single element selected." but in none of the guides do I have to have more than the UIImageView selected to set constraints.

Edit: added images:

ContraintsGreyedOut1of2

ContraintsGreyedOut2of2

Upvotes: 16

Views: 13506

Answers (8)

Marc Johnson
Marc Johnson

Reputation: 75

In my situation I had constraint objects on the storyboard that I wanted to link to constraints on a view within my view controller and the order I linked them in is what allowed me to link them.

If I had already linked the view object to a view in the view controller, I could not add constraints to the object in the storyboard, and by not being able to do that couldn't associate those constraints to the ones on the view controller.

But if I created the constraints on the view object before I associated it to the view on the controller, then I could then link the constraints 1st and then link the view itself afterwards.

Upvotes: 0

Akif Acar
Akif Acar

Reputation: 93

I confronted with the same situation. But in my case, I tried to insert imageview into horizontal stack view. Strangely, I could not add constraints for width and height value for imageview. I was able add the constraints only after deleting the current and adding a new imageview.

Upvotes: 1

Dan Alboteanu
Dan Alboteanu

Reputation: 10232

if you move a View around (eg. move it IN then OUT a TableView) then it will mysteriously loose its ability to accept constraints. So build a new one. XCode 11.4.1

Upvotes: 5

atalayasa
atalayasa

Reputation: 3480

In my case, Xcode 11.2 somehow set layout property of my UIButton tranlates mask into constraints. So I changed it to automatic from size inspector and it is fixed.

Upvotes: 52

rkyr
rkyr

Reputation: 3241

This is because you remove root view from view controller. When you add UIViewController to storyboard you can see this:

enter image description here

If you delete this view and add UIImageView you see this:

enter image description here

enter image description here

So add UIView as root and UIImageView as single subview of root view. And then you can play with constraints:

enter image description here

Upvotes: 3

Davyd Geyl
Davyd Geyl

Reputation: 4623

Auto layout constraints are either relationships between two objects or size constraints. You have only one object in this example, so no auto layout constraints are applicable here. Size constraints are also not applicable because this is the root view of your xib.

Not sure what you are trying to achieve here, but you would have full set of auto layout constraints if you placed your UIImageView on the content view of this view controller instead of changing its class.

Upvotes: 1

AMayes
AMayes

Reputation: 1767

The reason you can't set the constraints is because your image view is not in a view, it is the base view. In order to set constraints on a view of any kind, it must be inside another view.

Upvotes: 2

Agustin
Agustin

Reputation: 166

 UIImageView Selected + Add New Constraints button

All I did, clean and simple was: - At your view, add the UIImageView - Select your UIImageView - Click on the "Add New Constraints" button (shown in the attached picture).

As you can see, the UIImageView is selected.

The only possible explanation is that you may have forgotten to select it.

Cheers

Upvotes: 0

Related Questions