Categories
constraints ios nslayoutconstraint swift

add constraints programmatically swift

I am trying to add constraints to a facebook sdk login button.

I have the button inside a scroll view and I am trying to add a top constraint to a label that is also in the scroll view. I am able to successfully add the height constraint with no run time errors but the actual constraint does not seem to be applied to the button.

@IBOutlet weak var orLbl: UILabel!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
var loginFBButton = FBSDKLoginButton()
loginFBButton.readPermissions = ["public_profile", "email"]
let heightConstraint = NSLayoutConstraint(
item: loginFBButton,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1,
constant: 41)
let topConstraint = NSLayoutConstraint(
item: loginFBButton,
attribute: NSLayoutAttribute.TopMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: self.orLbl,
attribute: NSLayoutAttribute.BottomMargin,
multiplier: 1,
constant: 31)
let leadingConstraint = NSLayoutConstraint(
item: loginFBButton,
attribute: NSLayoutAttribute.Leading,
relatedBy: NSLayoutRelation.Equal,
toItem: self.registerButton,
attribute: NSLayoutAttribute.Left,
multiplier: 1,
constant: 0)
let trailingConstraint = NSLayoutConstraint(
item: loginFBButton,
attribute: NSLayoutAttribute.Leading,
relatedBy: NSLayoutRelation.Equal,
toItem: self.registerButton,
attribute: NSLayoutAttribute.Right,
multiplier: 1,
constant: 0)
self.scrollView.addSubview(loginFBButton)
//loginFBButton.center = self.scrollView.center
loginFBButton.addConstraints([heightConstraint, topConstraint])
}

Then when I include the addition of the top constraint, I am getting a runtime error:

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.

Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That’s illegal.

Both the label and the facebook button are in my scroll view? I’ve even printed out orLbl.superview and loginFBButton.superview and I am getting optional uiscrollview for both