AutoLayout automatic layout, NSLayoutConstraint view constraints, autolayout Layout

Source: Internet
Author: User

AutoLayout automatic layout, NSLayoutConstraint view constraints, autolayout Layout

I. Method

 NSLayoutConstraint *constraint =  [NSLayoutConstraint constraintWithItem:<#(id)#> attribute:<#(NSLayoutAttribute)#> relatedBy:<#(NSLayoutRelation)#> toItem:<#(id)#> attribute:<#(NSLayoutAttribute)#> multiplier:<#(CGFloat)#> constant:<#(CGFloat)#>]

 

Item:Control to be bound Attribute:Type of control constraints to be constrained (what constraints are imposed) RelatedBy:Relationship with reference controls ToItem:Reference controls Attribute:Type of control constraints referenced (constraints) Multiplier:The number of links between the restricted control and the referenced control. Constant:Constant The relationship between the last two parameters is as follows: View1.property1 = (view2.property2 * multiplier) + constant

 

Ii. Common Warnings and errors of Autolayout

1. the frame of the warning control does not match the added constraint. For example, the width of the control is 150, and the current width of the control is 160. the error lacks necessary constraints. For example, only the width and height are restricted, and the two constraints are not constrained. For example, the width of one constraint control is 150, the width of one constraint control is 160

Iii. Example

1. Code implementation:

ViewController. m

1 # import "ViewController. h "2 3 @ interface ViewController () 4 5 @ end 6 7 @ implementation ViewController 8 9-(void) viewDidLoad {10 [super viewDidLoad]; 11 // create and add blueview12 UIView * blueView = [[UIView alloc] init]; 13 blueView. backgroundColor = [UIColor blueColor]; 14 blueView. translatesAutoresizingMaskIntoConstraints = NO; // Autoresizing15 [self. view addSubview: blueView]; 16 17 // Add redView18 UIView * redView = [[UIView alloc] init]; 19 redView. backgroundColor = [UIColor redColor]; 20 redView. translatesAutoresizingMaskIntoConstraints = NO; 21 [self. view addSubview: redView]; 22 23 // 1.0 left constraint of the parent View 3024 NSLayoutConstraint * blueLeftCon = [NSLayoutConstraint constraintWithItem: blueView attribute: Invalid relatedBy: specified toItem: self. view attribute: NSLayoutAttributeLeft multiplier: 1.0 constant: 30.0]; 25 [self. view addConstraint: blueLeftCon]; 26 27 // 1.1 parent View top constraint 3028 NSLayoutConstraint * blueTopCon = [NSLayoutConstraint constraintWithItem: blueView attribute: Invalid relatedBy: Required toItem: self. view attribute: NSLayoutAttributeTop multiplier: 1.0 constant: 30.0]; 29 [self. view addConstraint: blueTopCon]; 30 31 // 1.2 constraint on the right of the parent View 3032 NSLayoutConstraint * blueRightCon = [NSLayoutConstraint constraintWithItem: blueView attribute: constraint relatedBy: constraint toItem: self. view attribute: NSLayoutAttributeRight multiplier: 1.0 constant:-30]; 33 [self. view addConstraint: blueRightCon]; 34 35 // 1.3 blueView equivalent to setting the height to 5036 NSLayoutConstraint * constraint = [constraint constraintWithItem: blueView attribute: constraint relatedBy: constraint toItem: nil attribute: 0 multiplier: 0 constant: 50]; 37 [blueView addConstraint: seconds]; 38 39 // 2.0 distance between the top and bottom of the redView: 2040 rows * redTopCon = [NSLayoutConstraint constraintWithItem: redView attribute: Latest relatedBy: NSLayoutRelationEqual toItem: blueView attribute: NSLayoutAttributeBottom multiplier: 1.0 constant: 20]; 41 [self. view addConstraint: redTopCon]; 42 43 // The left and left sides of the redView align with the blueView horizontal center. 44 NSLayoutConstraint * redLeftCon = [NSLayoutConstraint attributes: redView attribute: Latest relatedBy: Drawing toItem: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0]; 45 [self. view addConstraint: redLeftCon]; 46 47 // the right side of the redView is aligned with 48 rows * redRightCon = [NSLayoutConstraint constraintWithItem: redView attribute: specified relatedBy: specified toItem: blueView attribute: NSLayoutAttributeRight multiplier: 1.0 constant: 0]; 49 [self. view addConstraint: redRightCon]; 50 51 // 2.3 redView height reference blueView height equal 52 bytes * redHeightCon = [NSLayoutConstraint constraintWithItem: redView attribute: Invalid relatedBy: Invalid toItem: blueView attribute: NSLayoutAttributeHeight multiplier: 1.0 constant: 0]; 53 [self. view addConstraint: redHeightCon]; 54 55} 56 57 @ endView Code

 

2. storyboard implementation:

Effect:

 

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.