IOS AutoLayout Encoding

Source: Internet
Author: User

// Do not define the Frame in this place. The following Visual Format defines the Frame.

UIView * redView = [[UIView alloc] init];

RedView. backgroundColor = [UIColor redColor];


// Set NO according to the Apple Doc to prevent the autoresize conflict between the Constraint and View.

[RedView setTranslatesAutoresizingMaskIntoConstraints: NO];

[Self. view addSubview: redView];

UIView * blueView = [[UIView alloc] init];

BlueView. backgroundColor = [UIColor blueColor];

[BlueView setTranslatesAutoresizingMaskIntoConstraints: NO];

[Self. view addSubview: blueView];

// Bind two views

NSDictionary * views = NSDictionaryOfVariableBindings (redView, blueView );


// The constraint is used to define the horizontal direction correlation (x, width) of the Frame)

[Self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-(> = 10)-[redView (200)]"

Options: 0

Metrics: nil

Views: views];


// The constraint is used to define the vertical correlation (y, height) of the Frame)

[Self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "V: |-(> = 240)-[redView (100)]"

Options: 0

Metrics: nil

Views: views];

[Self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "H: [blueView (= redView)]"

Options: 0

Metrics: nil

Views: views];

[Self. view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @ "V: |-(40)-[blueView (= redView)]"

Options: 0

Metrics: nil

Views: views];

// The constraint here is used to describe the relationship between the two views.

[Self. view addConstraint: [NSLayoutConstraint constraintWithItem: blueView

Attribute: NSLayoutAttributeLeft

RelatedBy: NSLayoutRelationEqual

ToItem: redView

Attribute: NSLayoutAttributeLeft

Multiplier: 1

Constant: 0];

Above.

Method and Macro do not provide an explanation. Please refer to the Apple Doc.


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.