Reputation: 1608
Why am I getting a Thread 1: signal SIGABRT
?
I am trying to learn how to delete and create new constraints to UIViews
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var red: UIView!
@IBOutlet weak var green: UIView!
@IBOutlet weak var redTop: NSLayoutConstraint!
@IBOutlet weak var greenToRed: NSLayoutConstraint!
@IBAction func shift(sender: AnyObject) {
green.removeConstraint(greenToRed)
let newGreen = NSLayoutConstraint(item: green, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)
green.addConstraint(newGreen)
newGreen.active = true
red.removeConstraint(redTop)
let newRed = NSLayoutConstraint(item: red, attribute: .Top, relatedBy: .Equal, toItem: green, attribute: .Bottom, multiplier: 1, constant: 10)
red.addConstraint(newRed)
newRed.active = true} }
Upvotes: 0
Views: 104
Reputation: 104082
Constraints on a subview need to be added to the superview of the view involved. So a constraint between green and view needs to be added to view (since it's the superview of green). Similarly, a constraint between red and green also needs to be added to their common superview, view (the controllers self.view).
From Apple's documentation: "To make a constraint active, you must add it to a view. The view that holds the constraint must be an ancestor of the views the constraint involves, and should usually be the closest common ancestor. (This is in the existing NSView API sense of the word ancestor, where a view is an ancestor of itself.)".
Upvotes: 1