Ios 将约束应用于facebook登录按钮

Ios 将约束应用于facebook登录按钮,ios,swift,uiview,autolayout,nslayoutconstraint,Ios,Swift,Uiview,Autolayout,Nslayoutconstraint,我试图在facebook按钮上添加限制,使其成为按钮,这是我的代码 ... let loginButton = FBSDKLoginButton() loginButton.readPermissions = ["public_profile", "email"] loginButton.center = self.view.center let myConstraint = NSLayoutConstraint(item: loginButton, attribute: NS

我试图在facebook按钮上添加限制,使其成为按钮,这是我的代码

...
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.center = self.view.center

let myConstraint =
NSLayoutConstraint(item: loginButton,
        attribute: NSLayoutAttribute.Bottom,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.view,
        attribute: NSLayoutAttribute.Bottom,
        multiplier: 1.0,
        constant: -20)
    self.view.addConstraint(myConstraint)

loginButton.addConstraint(myConstraint)

loginButton.delegate = self
self.view.addSubview(loginButton)
它失败了,错误如下

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x15fd258a0 FBSDKLoginButton:0x15fe562d0'Log in'.bottom == UIView:0x15fd2fa60.bottom - 20>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
未为约束准备视图层次结构:
添加到视图时,约束项必须是该视图(或视图本身)的后代。如果在装配视图层次之前需要解决约束,则此操作将崩溃。中断-[UIView(UIConstraintBasedLayout)\u viewHierarchyUnpreparedForConstraint:]以进行调试。

有什么想法吗?

错误告诉您添加约束太早了。您应该首先添加
登录按钮
,然后再添加约束:

loginButton.delegate = self

self.view.addSubview(loginButton)
self.view.addConstraint(myConstraint)
loginButton.addConstraint(myConstraint)

错误消息提到,当添加到视图时,约束的项必须是该视图的后代。但是在将实际按钮添加到视图之前,您添加了
登录按钮的约束。

错误告诉您添加约束太早。您应该首先添加
登录按钮
,然后再添加约束:

loginButton.delegate = self

self.view.addSubview(loginButton)
self.view.addConstraint(myConstraint)
loginButton.addConstraint(myConstraint)
错误消息提到,当添加到视图时,约束的项必须是该视图的后代。但是,在将实际按钮添加到视图之前,需要添加
loginButton
的约束