ios Swift 以编程方式添加约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35452908/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:32:21  来源:igfitidea点击:

Swift add constraint programmatically

iosiphoneswift

提问by jimshicard

I add a UILabel(amountLabel) in UIViewControllerin storyboard editor. And then in swift file, viewDidLoad, I programatically create a UITextField(paymentTextField) and try to add a constraint between amountLabeland paymentTextField. Here is my code in viewDidload:

我在故事板编辑器中添加了一个UILabel(amountLabel) UIViewController。然后在 swift 文件中viewDidLoad,我以编程方式创建一个UITextField(paymentTextField) 并尝试在amountLabel和之间添加约束paymentTextField。这是我的代码viewDidload

let paymentTextField = UITextField()
    paymentTextField.translatesAutoresizingMaskIntoConstraints = false
    paymentTextField.frame = CGRectMake(15, 100, CGRectGetWidth(self.view.frame) - 30, 44)
    self.view.addSubview(paymentTextField)

    let bottonConstraint = NSLayoutConstraint(item: paymentTextField, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.amountLabel , attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 30)
    bottonConstraint.identifier = "paymentTextFieldSpacing"
    NSLayoutConstraint.activateConstraints([bottonConstraint])

But I get an error:

但我收到一个错误:

"Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with items > and > because they have no common ancestor. Does the constraint reference items in different view hierarchies? That's illegal."

“由于未捕获的异常‘NSGenericException’而终止应用程序,原因:‘无法使用项目 > 和 > 激活约束,因为它们没有共同的祖先。约束是否引用了不同视图层次结构中的项目?这是非法的。”

Does anyone know what is wrong? amountLabelis directly dragged to the view in storyboard and "paymentTextField" is added programmatically to the same view. Why have they no common ancestor?

有谁知道出了什么问题?amountLabel直接拖到情节提要中的视图中,并以编程方式将“paymentTextField”添加到同一视图中。为什么他们没有共同的祖先?

回答by Jamil Nawaz

I ran into the same problem that you described earlier. In order to make the programmatic subview, (in your case the paymentTextField) you have to add this to the subview first and then apply your constraints.

我遇到了你之前描述的同样问题。为了制作程序化子视图(在您的情况下是 paymentTextField),您必须先将其添加到子视图中,然后再应用您的约束。

By adding the subview to view first, this ensures both views have the same parent.

通过首先将子视图添加到视图中,这可以确保两个视图具有相同的父视图。

回答by Milan Kamilya

Checklist for this ISSUE:

此问题的清单:

  • Check whether you added the programmatically created view to its parent before activating constraints
  • Check whether you write constraints activation code inside viewDidLoad() / viewWillAppear(). You should write constraints activation code in updateViewConstraints or viewWillLayoutSubviews. ( suggested by vmeyer)
  • Check whether you turn off translatesAutoresizingMaskIntoConstraints.
  • 在激活约束之前检查是否以编程方式创建的视图添加到其父视图
  • 检查您是否在viewDidLoad() / viewWillAppear() 中编写了约束激活代码。您应该在updateViewConstraints 或 viewWillLayoutSubviews 中编写约束激活代码 。(由 vmeyer 建议
  • 检查您是否关闭translatesAutoresizingMaskIntoConstraints

回答by Pauls

The error states that "because they have no common ancestor", which means that they don't share the same parent. In order to relate constraint between two items, they have to have a child-parent relationship or a sibling one.

错误指出“因为他们没有共同的祖先”,这意味着他们不共享同一个父级。为了关联两个项目之间的约束,它们必须具有子父关系或兄弟关系。

In your case just make sure they have the same parent view before adding the constraint programmatically.

在您的情况下,只需确保它们在以编程方式添加约束之前具有相同的父视图。

回答by happyhippie924

Make sure you added the paymentTextFieldto your view:

确保您添加paymentTextField到您的视图:

paymentTextField.translatesAutoResizingMaskIntoConstraints = false
view.addSubview(paymentTextField)

...

Add your constraints, for example paymentTextField.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true

添加您的约束,例如 paymentTextField.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true

回答by srisivarakrishnavaraprasad

let nameLabel: UILabel = {

   let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

if you forgot setting this label.translatesAutoresizingMaskIntoConstraints = falseor if you add subview after constraints this problem arises

如果您忘记设置此项label.translatesAutoresizingMaskIntoConstraints = false或在约束后添加子视图,则会出现此问题