简体   繁体   English

Xcode 6.4 - 无法对视图或......任何东西设置约束

[英]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有很多关于自动布局的指南,例如: 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.现在约束似乎并不特别复杂,除了当尝试在 ViewController 中的 UIImageView 上设置它们时,勾选“使用自动布局”后,我一生都无法设置任何约束我单击用于实现此目的的三个按钮中的任何一个,用于设置约束的所有选项都是灰色的。

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.但在所有指南中,我都不需要选择 UIImageView 来设置约束。

Edit: added images:编辑:添加图像:

限制GreyedOut1of2

限制GreyedOut2of2

In my case, Xcode 11.2 somehow set layout property of my UIButton tranlates mask into constraints .就我而言,Xcode 11.2 以某种方式设置了我的UIButton布局属性,将tranlates mask into constraints So I changed it to automatic from size inspector and it is fixed.所以我把它从尺寸检查器改为automatic ,它是固定的。

if you move a View around (eg. move it IN then OUT a TableView) then it will mysteriously loose its ability to accept constraints.如果您四处移动 View(例如,将其移入然后移出 TableView),那么它将神秘地失去接受约束的能力。 So build a new one.所以建立一个新的。 XCode 11.4.1 Xcode 11.4.1

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.为了对任何类型的视图设置约束,它必须在另一个视图中。

This is because you remove root view from view controller.这是因为您从视图控制器中删除了根视图。 When you add UIViewController to storyboard you can see this:当您将 UIViewController 添加到故事板时,您可以看到:

在此处输入图片说明

If you delete this view and add UIImageView you see this:如果您删除此视图并添加 UIImageView,您会看到:

在此处输入图片说明

在此处输入图片说明

So add UIView as root and UIImageView as single subview of root view.因此,将 UIView 添加为 root 并将 UIImageView 添加为 root view 的单个子视图。 And then you can play with constraints:然后你可以玩约束:

在此处输入图片说明

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.大小限制也不适用,因为这是您的 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.不确定您要在这里实现什么,但是如果您将 UIImageView 放置在此视图控制器的内容视图上而不是更改其类,那么您将拥有全套自动布局约束。

UIImageView Selected + Add New Constraints 按钮

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).我所做的一切,干净而简单的是: - 在您的视图中,添加 UIImageView - 选择您的 UIImageView - 单击“添加新约束”按钮(如附图所示)。

As you can see, the UIImageView is selected.如您所见,选择了 UIImageView。

The only possible explanation is that you may have forgotten to select it.唯一可能的解释是您可能忘记选择它。

Cheers干杯

I confronted with the same situation.我遇到了同样的情况。 But in my case, I tried to insert imageview into horizontal stack view.但就我而言,我尝试将 imageview 插入到水平堆栈视图中。 Strangely, I could not add constraints for width and height value for imageview.奇怪的是,我无法为 imageview 的宽度和高度值添加约束。 I was able add the constraints only after deleting the current and adding a new imageview .只有在删除当前并添加新的 imageview后,我才能添加约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM