IOS AutoLayout code Implementation constraints-VFL

Source: Internet
Author: User

Under AutoLayout, although using IB to drag and drop controls, it is still not possible to use code to create controls, which are constraints that require code.

IOS provides two ways to add constraints

The first type:

+ (Instancetype) Constraintwithitem: (id) view1 attribute: (nslayoutattribute) attr1 Relatedby: (nslayoutrelation) Relation Toitem: (id) view2 attribute: (nslayoutattribute) attr2 multiplier: (cgfloat) Multiplier constant: (cgfloat) C;

The second type:

+ (Nsarray *) Constraintswithvisualformat: (NSString *) format options: (nslayoutformatoptions) OPTs metrics: ( Nsdictionary *) Metrics Views: (Nsdictionary *) views;

The first method returns a constraint object, which is a constraint. See the parameters are also better understood, is to set a constraint between View1 and View2, but often a control needs a few constraints to constrain, plus such a long way, so that a control requires a lot of code to implement constraints.

The second method returns an array containing a set of constraints, so a control is typically called two times and the second method can implement the constraint. So this method is more concise, but this method uses a descriptive language, a bit difficult to understand, the following is a brief introduction to the second method of use .

These parameters are described first:

(nsdictionary*) Views: Indicates that the set of constraints involved in the relevant sub-view, such as an interface with a, b two controls, this set of constraints related to both controls, then the dictionary type A, b control as a parameter views, can be used Nsdictionaryofvariablebindings (A, B) equivalent to [nsdictionary dictionarywithobjectsandkeys:a @ "A", B, @ "B", nil];

(nsdictionary*) Metrics: For some specific widths and heights, we can store them directly in a dictionary, so that in descriptive languages we can directly use keys to describe the width of the height. Such as:

Nsdictionary *metrics = @{@ "Buttonwidth": @200.0};

(nslayoutformatoptions) OPTs: See Enumeration type, explanation section. These are the usual: NSLayoutFormatAlignAllLeft //控件之间左对齐 NSLayoutFormatAlignAllRight //控件之间右对齐 NSLayoutFormatAlignAllTop //...上对齐 NSLayoutFormatAlignAllBottom //...下对齐 NSLayoutFormatAlignAllLeading  // 使所有视图根据当前区域文字开始的边缘对齐(英语:左边,希伯来语:右边) NSLayoutFormatAlignAllTrailing  // 使所有视图根据当前区域文字结束的边缘对齐(英语:右边,希伯来语:左边)。 NSLayoutFormatAlignAllCenterX  // 使所有视图通过设置中心点的 X 值彼此相等来对齐。(nsstring*) format This is the core VFL statement, for example (h means horizontal, V for vertical) H:|-[view]-|         //view与superview的左右边界为标准间距 H:|-[view]          //view与superview的左边界为标准间距,右边不处理 H:|[view]           //view与superview的左边界对齐,右边不处理 H:|-20.0-[view]-30.0f-|   //view与superview的左边边界间距分别为20,和30 H:[view(100.0)]       //view宽度为100 H:|-[button1(button2)]-[button2]-|  //button1与button2等宽,之间为标准间距..... V:|-20.0-[view(30.0)]  //view距顶部边界20,自身高度为30.0

Note: When writing the left or right margin of two controls, the order in which the controls are written is written in the order of the UI interface.

i.e. @ "h:[_ Nametextfield]-20-[button] "means _nametextfieldbutton spacing between 20,_ Nametextfieldbutton

@ "H:[button]-20-[_nametextfield]" means that the distance between the button and the _nametextfield is button on the left,_nametextfield on the right.

Note: When writing the distance between two controls, you cannot add two to the left and right sides | |

To implement the constraints, we only need to initialize the controls first, but we don't need to initialize them with alloc in AutoLayout, and turn off autoresizing constraints UIView *view = [self  new ]; view.translatesAutoresizingMaskIntoConstraints = NO;Ps:demo is still in the process of finishing, ready to upload immediately.

IOS AutoLayout code Implementation constraints-VFL

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.