NSLayoutConstraints Margin and view that is not displayed

3

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.

    
asked by MatiEzelQ 07.06.2016 в 18:45
source

1 answer

2

If you use% normal% co, you should not put frame of% property false . Therefore, you must delete the following line:

myRedView.translatesAutoresizingMaskIntoConstraints = false

Tested at translatesAutoresizingMaskIntoConstraints and works perfectly.

    
answered by 07.06.2016 / 23:35
source