What's wrong with this code? Because the blue view is not shown?
let myRedView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
myRedView.translatesAutoresizingMaskIntoConstraints = false
myRedView.backgroundColor = UIColor.redColor()
let myBlueView = UIView()
myBlueView.translatesAutoresizingMaskIntoConstraints = false
myBlueView.backgroundColor = UIColor.blueColor()
myRedView.addSubview(myBlueView)
let blueY = NSLayoutConstraint(item: myBlueView, attribute: .Left, relatedBy: .Equal, toItem: myRedView, attribute: .Right, multiplier: 1.0, constant: 10)
let blueX = NSLayoutConstraint(item: myBlueView, attribute: .Top, relatedBy: .Equal, toItem: myRedView, attribute: .Top, multiplier: 1.0, constant: 10)
//let blueWidth = NSLayoutConstraint(item: myBlueView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 200)
//let blueHeight = NSLayoutConstraint(item: myBlueView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 200)
myRedView.addConstraints([blueX,blueY])
With or without the width and the height, it is not shown and I do not understand the reason.
Another question I have about these constraints is to whom I should apply the .LeftMargin
. If the main view on which I am modeling or the second.