Swift Development: Nslayoutconstraint Pure code for automatic layout-introductory article

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.