Xcode 6.4 - 无法对视图或......任何东西设置约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32514185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 6.4 - Unable to set constraints on Views or ... anything
提问by Lance
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:
编辑:添加图像:
采纳答案by rkyr
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. And then you can play with constraints:
因此,将 UIView 添加为根视图,将 UIImageView 添加为根视图的单个子视图。然后你可以玩约束:
回答by atalayasa
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.
就我而言,Xcode 11.2 以某种方式设置了我的UIButton
tranlates mask into constraints
. 因此,我将其更改为automatic
from size inspector 并已修复。
回答by AMayes
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.
您无法设置约束的原因是因为您的图像视图不在视图中,它是基本视图。为了对任何类型的视图设置约束,它必须在另一个视图内。
回答by Davyd Geyl
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 放置在此视图控制器的内容视图上而不是更改其类,那么您将拥有全套自动布局约束。
回答by Agustin
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
干杯
回答by Dan Alboteanu
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
如果您四处移动 View(例如,将其移入然后移出 TableView),那么它将神秘地失去接受约束的能力。所以建立一个新的。Xcode 11.4.1