xcode 无法在 tableview 中使用自动布局约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21182226/
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
Unable to use autolayout constraints in a tableview
提问by ZviBar
I have this table view and am unable to add any autolayout constraints. The control drag doesn't work - no constraints show up, and the builder form seems inactive, I can't set any values in there (see below). Autolayout is enabled everywhere across the project, basically on every view. Why is that?
我有这个表视图,无法添加任何自动布局约束。控件拖动不起作用 - 没有显示任何约束,并且构建器表单似乎处于非活动状态,我无法在其中设置任何值(见下文)。自动布局在整个项目的任何地方都启用,基本上是在每个视图上。这是为什么?
回答by Sergey Demchenko
It is because you selected cell or Content View in cell. You can add constrains only to views inside Content View.
这是因为您在单元格中选择了单元格或内容视图。您只能向内容视图中的视图添加约束。
UPDATE
更新
Also you may not be able to add constraints to view if this view is top level object (is not a child of parent view).
此外,如果此视图是顶级对象(不是父视图的子视图),则您可能无法向视图添加约束。
UPDATE 2
更新 2
You can add auto layout constraints only to UIViews and their subclasses. In your case you try to add constrains to UITextField which is located inside UINavigationItem (it is not subclass of UIView).
您只能向 UIViews 及其子类添加自动布局约束。在您的情况下,您尝试向位于 UINavigationItem 内的 UITextField 添加约束(它不是 UIView 的子类)。
Here you can find the similar issue and answer on it: iOS Autolayout and UIToolbar/UIBarButtonItems
在这里你可以找到类似的问题和答案:iOS Autolayout and UIToolbar/UIBarButtonItems
回答by brian.clear
Make sure the Cell Style is "Custom" and Add New Constraints should be enabled.
确保单元格样式为“自定义”并启用添加新约束。
I had a Cell with Style 'Basic' any I was able to customise the label and add a UITableViewCell subclass. I had accidentally linked my outlet to the UILabel in the default BASIC style cell.
我有一个样式为“基本”的单元格,我可以自定义标签并添加 UITableViewCell 子类。我不小心将我的插座链接到默认 BASIC 样式单元格中的 UILabel。