[IOS development-113] AutoLayout is used on the storyboard to implement the AutoLayout layout method and simple animation with pure code. autolayout is developed for ios.

Source: Internet
Author: User

[IOS development-113] AutoLayout is used on the storyboard to implement the AutoLayout layout method and simple animation with pure code. autolayout is developed for ios.

(1) Use AutoLayout in storyboard. This AutoLayout conflicts with autoResizing and can only be selected.

-- The Mutual constraint between controls of different levels is added to the high-level level.

-- Mutual constraints of controls at the same level are added to their parent controls.

-- The Mutual constraint of different branch controls is the first common parent control added to them and traced back.

These three rules are useful in code creation. When storyboard is used, the system automatically adds it for us.



(2) Use code to implement AutoLayout. The step is to first create a layout constraint object and then add this object to the control to be constrained.

-- You must first disable the autoresizing function of the control to be constrained.

-- Before adding a constraint object, make sure that the control has been added to its parent control.

-(Void) viewDidLoad {[super viewDidLoad]; // ensure that the control to be constrained already has the parent control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; [self. view addSubview: blueView]; // disable the autoresizing blueView of the control to be constrained. constraint = NO; // define the constraint object and add it (note where to add it). The width and height are 100. Center NSLayoutConstraint * width = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeWidth relatedBy: required toItem: nil attribute: Invalid multiplier: 1.0 constant: 100]; [blueView addConstraint: width]; required * height = [NSLayoutConstraint constraintWithItem: blueView attribute: Required relatedBy: Required toItem: nil attribute: Lost multiplier: 1.0 constant: 100]; [blueView addConstraint: height]; optional * centerX = [NSLayoutConstraint constraintWithItem: blueView attribute: Lost relatedBy: Lost toItem: self. view attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0]; [self. view addConstraint: centerX]; NSLayoutConstraint * centerY = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeCenterY multiplier: 1.0 constant: 0]; [self. view addConstraint: centerY];}

(3) The code for mutual control constraints is the same as the code for the preceding single control, but note that the control is calculated relative to who; the constraint is added to itself or the parent control.


-(Void) viewDidLoad {[super viewDidLoad]; // ensure that the control to be constrained already has the parent control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; [self. view addSubview: blueView]; UIView * redView = [[UIView alloc] init]; redView. backgroundColor = [UIColor redColor]; [self. view addSubview: redView]; // disable the autoresizing blueView of the control to be constrained. translatesAutoresizingMaskIntoConstraints = NO; redView. failed = NO; // first adjust blueView NSLayoutConstraint * height = [NSLayoutConstraint constraintWithItem: blueView attribute: Invalid relatedBy: Too toItem: nil attribute: Too multiplier: 1.0 constant: 40]; [blueView addConstraint: height]; NSLayoutConstraint * left = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeLeft relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeLeft multiplier: 1.0 constant: 20]; [self. view addConstraint: left]; // note-20 NSLayoutConstraint * right = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeRight relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeRight multiplier: 1.0 constant:-20]; [self. view addConstraint: right]; NSLayoutConstraint * top = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem: self. view attribute: NSLayoutAttributeTop multiplier: 1.0 constant: 20]; [self. view addConstraint: top]; // adjust redView NSLayoutConstraint * heightR = [NSLayoutConstraint failed: redView attribute: Unknown relatedBy: Hangzhou toItem: blueView attribute: Hangzhou multiplier: 1.0 constant: 0]; [self. view addConstraint: heightR]; optional * widthR = [NSLayoutConstraint constraintWithItem: redView attribute: descrirelatedby: descritoitem: blueView attribute: descrimultiplier: 0.5 constant: 0]; [self. view addConstraint: widthR]; NSLayoutConstraint * rightR = [NSLayoutConstraint failed: redView attribute: descrirelatedby: descritoitem: blueView attribute: descrimultiplier: 1.0 constant: 0]; [self. view addConstraint: rightR]; // note that redView top is the NSLayoutConstraint * topR = [NSLayoutConstraint failed: redView attribute: Latest relatedBy: Invalid toItem: blueView attribute: NSLayoutAttributeBottom multiplier: 1.0 constant: 20]; [self. view addConstraint: topR];}

(4) use the VFL language, namely visual format language.

-- The control must be included in [].

-- | Indicates the boundary of the parent control.

-- Multiplication and division are inconvenient to use, and constraintWithItem must be used as an aid.


-(Void) viewDidLoad {[super viewDidLoad]; // ensure that the control to be constrained already has the parent control UIView * blueView = [[UIView alloc] init]; blueView. backgroundColor = [UIColor blueColor]; [self. view addSubview: blueView]; UIView * redView = [[UIView alloc] init]; redView. backgroundColor = [UIColor redColor]; [self. view addSubview: redView]; // disable the autoresizing blueView of the control to be constrained. translatesAutoresizingMaskIntoConstraints = NO; redView. translatesAutoresizingMaskIntoConstraints = NO; // The VFL language is used, but some cannot be implemented, such as a multiple of the width, because NSArray * cons0 = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-20-[blueView]-20-| "options: 0 metrics: nil views: @ {@" blueView ": blueView}]; [self. view addConstraints: cons0]; NSArray * cons1 = [NSLayoutConstraint constraintsWithVisualFormat: @ "V: |-20-[blueView (40)]-20-[redView (= blueView)] "options: NSLayoutFormatAlignAllRight metrics: nil views :@{@" blueView ": blueView, @" redView ": redView}]; [self. view addConstraints: cons1]; NSArray * cons2 = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: [redView (= blueView)]" options: 0 metrics: nil views: @ {@ "blueView": blueView, @ "redView": redView}]; [self. view addConstraints: cons2];}

-- The option is used when there are two controls that need to be aligned. You can use multiple | symbols.

-- Metrics stores placeholder symbols and value dictionaries. For example, we can also write an NSDictionary dictionary for storage as follows:

NSArray *cons0=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[blueView]-margin-|" options:0 metrics:@{@"margin":@20} views:@{@"blueView":blueView}];

-- Views can be easily written as follows: Use NSDictionaryOfVariableBindings () to create a key-value pair.

NSArray *cons0=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[blueView]-margin-|" options:0 metrics:@{@"margin":@20} views:NSDictionaryOfVariableBindings(blueView,redView)];

(5) AutoLayout allows the Label to automatically increase the height based on the content. Of course, the number of lines of the label must be set to 0 to wrap the line.


(6) AutoLayout can also be used as an animation to change the value of the constraint.

-- The animation effect also needs the combination of animationWithDuration of UIView. Note that the constraint value needs to be modified first, and then layoutIfNeeded is called in the animation method.

-- Only the constant can be modified, while other values are read-only and cannot be modified.

#import "ViewController.h"@interface ViewController ()@property(nonatomic,strong) NSLayoutConstraint *con3;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    .....    NSLayoutConstraint *con3=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];    [self.view addConstraint:con3];    self.con3=con3;}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    self.con3.constant=100;    [UIView animateWithDuration:1.0 animations:^{        [self.view layoutIfNeeded];    }];}

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.