Requires A view with a width of 200, through code, so that the view is 20 margins from the lower-right corner of the parent control
/* Constraint settings , the control internal constraints are added by themselves, such as the width of the height, if the other
Control constraint Then there is a parent control to add
* Create constraint nslayoutconstraint parameter description:
* Item yourself
* Attribute
* Relatedby greater than or equal to or equal to
* Toitem Another control
* Attribute Another control's familiarity
* Multiplier multiplied by how much
* Constant: plus how much
* Nslayoutconstraint: property value of one control equals property value of another control
Multiplied by how many plus how much
* Add constraint addconstraint
*/
Swift code:
Let Blueview =UIView ();
Blueview. backgroundcolor =uicolor. Bluecolor ()
self. View. Addsubview(Blueview)// system will default to autoresizing constraints
Shutdown autoresizing do not close or the program crashes
Blueview.translatesautoresizingmaskintoconstraints =false
// width constraint
Let width:nslayoutconstraint =nslayoutconstraint (Item:blueview, attribute: nslayoutattribute. Width, Relatedby:nslayoutrelation. Equal, Toitem:nil, attribute: nslayoutattribute. Notanattribute, multiplier:0.0, constant:
Blueview. addconstraint (width)// Add constraint yourself
// height constraints
Let height:nslayoutconstraint =nslayoutconstraint (Item:blueview, attribute: nslayoutattribute. Height, Relatedby:nslayoutrelation. Equal, Toitem:nil, attribute: nslayoutattribute. Notanattribute, multiplier:0.0, constant:
Blueview. addconstraint (height)// Add constraint yourself
// right constraint
Let right :nslayoutconstraint =nslayoutconstraint (Item:blueview, attribute: nslayoutattribute. Right, Relatedby:nslayoutrelation. Equal, Toitem: Self. View, attribute:nslayoutattribute. Right, multiplier:1.0, constant:--)
Blueview. superview!. AddConstraint (right)// parent control Add constraint
// bottom constraint
Let bottom:nslayoutconstraint =nslayoutconstraint (Item:blueview, attribute: nslayoutattribute. Bottom, Relatedby:nslayoutrelation. Equal, Toitem: Self. View, attribute:nslayoutattribute. Bottom, Multiplier:1.0, constant:--)
Blueview. superview!. addconstraint (bottom)// parent control Add constraint
:
Swift Development: Nslayoutconstraint Pure code for automatic layout-introductory article