IOS_AutoLayout, iosautolayout code

Source: Internet
Author: User

IOS_AutoLayout, iosautolayout code
1. You must disable autoresizing before using code to create AutoLayout.

/*** A control is centered in the parent control */-(void) centerTest {// 1. add control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; [self. view addSubview: blueView]; // 2. add constraints // disable autoresizing blueView. height = NO;/** width and height: 100 position: center in the parent control * // width = 100 NSLayoutConstraint * width = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: 100]; [blueView addConstraint: width]; // blueView height = 100 NSLayoutConstraint * height = [NSLayoutConstraint constraintWithItem: blueView attribute: descrirelatedby: descritoitem: nil attribute: descrimultiplier: 1.0 constant: 100]; [blueView addConstraint: height]; // CenterX = self. view CenterX * 1.0 + 0.0 NSLayoutConstraint * centerX = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0.0]; [self. view addConstraint: centerX]; // CenterY = self. view CenterY * 1.0 + 0.0 NSLayoutConstraint * centerY = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeCenterY multiplier: 1.0 constant: 0.0]; [self. view addConstraint: centerY];}



-(Void) viewDidLoad {[super viewDidLoad]; // 1. add control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; blueView. translatesAutoresizingMaskIntoConstraints = NO; [self. view addSubview: blueView]; UIView * redView = [[UIView alloc] init]; redView. backgroundColor = [UIColor redColor]; redView. translatesAutoresizingMaskIntoConstraints = NO; [self. view addSubview: redView]; // 2. constraint Blue // 2. 1. height limit * blueHeight = [NSLayoutConstraint constraintWithItem: blueView attribute: Comment relatedBy: Comment toItem: nil attribute: Comment multiplier: 1.0 constant: 40]; [blueView addConstraint: blueHeight]; // 2. 2. left margin CGFloat margin = 20; NSLayoutConstraint * blueLeft = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeLeft relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeLeft multiplier: 1.0 constant: margin]; [self. view addConstraint: blueLeft]; // 2. 3. top padding NSLayoutConstraint * blueTop = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeTop multiplier: 1.0 constant: margin]; [self. view addConstraint: blueTop]; // 2. 4. right margin NSLayoutConstraint * blueRight = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeRight relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeRight multiplier: 1.0 constant:-margin]; [self. view addConstraint: blueRight]; // 3. constraint red // 3. 1. right side of Red = NSLayoutConstraint * redRight = [NSLayoutConstraint constraintWithItem: redView attribute: Invalid relatedBy: Too toItem: blueView attribute: Too multiplier: 1.0 constant: 0.0]; [self. view addConstraint: redRight]; // 3. 2. make the red Height = blue height NSLayoutConstraint * redHeight = [NSLayoutConstraint constraintWithItem: redView attribute: descrirelatedby: descritoitem: blueView attribute: descrimultiplier: 1.0 constant: 0.0]; [self. view addConstraint: redHeight]; // 3. 3. make the red top = blue bottom + spacing NSLayoutConstraint * redTop = [NSLayoutConstraint constraintWithItem: redView attribute: Invalid relatedBy: Too toItem: blueView attribute: Too multiplier: 1.0 constant: margin]; [self. view addConstraint: redTop]; // 3. 4. make the red width = blue width * 0.5 NSLayoutConstraint * redWidth = [NSLayoutConstraint constraintWithItem: redView attribute: descrirelatedby: descritoitem: blueView attribute: descrimultiplier: 0.5 constant: 0.0]; [self. view addConstraint: redWidth];}








2. Create constraints in AutoLayout using VFL
The Effect and code of Example 1 are as follows:
// A redView under a blueView on the top. The left side of the redView is aligned with the center of the blueView-(void) VFLtest1 {// 1. add control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; blueView. translatesAutoresizingMaskIntoConstraints = NO; [self. view addSubview: blueView]; UIView * redView = [[UIView alloc] init]; redView. backgroundColor = [UIColor redColor]; redView. translatesAutoresizingMaskIntoConstraints = NO; [self. view addSubview: redView]; // 2. VFL generation constraints. Note that the object must be included in [] NSArray * conts = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-20-[blueView]-20-|" options: 0 metrics: nil views: @ {@ "blueView": blueView}]; [self. view addConstraints: conts]; NSArray * conts2 = [NSLayoutConstraint constraintsWithVisualFormat: @ "V: |-20-[blueView (40)]-20-[redView (= blueView)] "options: NSLayoutFormatAlignAllRight metrics: nil views :@{@" blueView ": blueView, @" redView ": redView}]; [self. view addConstraints: conts2]; // 3. note that VFL does not support syntax NSLayoutConstraint * redWidth = [NSLayoutConstraint constraintWithItem: redView attribute: inclurelatedby: inclutoitem: blueView attribute: inclumultiplier: 0.5 constant: 0.0]; [self. view addConstraint: redWidth];}


The Effect and code of Example 2 are as follows:
-(Void) viewDidLoad {[super viewDidLoad]; // 1. add control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; blueView. translatesAutoresizingMaskIntoConstraints = NO; [self. view addSubview: blueView]; UIView * redView = [[UIView alloc] init]; redView. backgroundColor = [UIColor redColor]; redView. translatesAutoresizingMaskIntoConstraints = NO; [self. view addSubview: redView]; // 2. use the following macro to generate constraints on VFL. Make sure that the key-value pair name is the same as NSDictionary * mertrics =@{@ "margin": @ 20 }; // Parameter \ value NSDictionary * views = NSDictionaryOfVariableBindings (blueView, redView); NSArray * conts = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-margin-[blueView]-margin-[redView (= blueView)]-margin-| "options: NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics: mertrics views: views]; NSArray * conts2 = [NSLayoutConstraint constraintsWithVisualFormat: @ "V: [blueView (= blueHeight)]-margin-|" options: 0 metrics: @ {@ "blueHeight": @ 40, @ "margin": @ 20} views: views]; [self. view addConstraints: conts]; [self. view addConstraints: conts2];}







3. Use constraints in AutoLayoutCreate an animation
Drag the constraints in the storyboard to the. m file to make it an IBOutlet object. For example:

@ Property (weak, nonatomic) IBOutletNSLayoutConstraint * leftMarginConstraint;

@ Property (weak, nonatomic) IBOutletNSLayoutConstraint * widthConstraint;

When an event is triggered, change its constant value, and then call the layoutIfNeeded method of the object where the constraint is located, fixed ~ For example:

Self. leftMarginConstraint. constant = 100;

Self. widthConstraint. constant = 200;

[UIViewanimateWithDuration: 2.0 animations: ^ {

[Self. viewlayoutIfNeeded];

[Self. redViewlayoutIfNeeded];

}];


4. We recommend that you use Top Layout Guide on the Top margin, that is, the lower line of the status bar, because iOS6 uses the lower line of the status bar as the origin, while iOS7 and above use the upper left corner of the screen as the origin.



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.