IOS intermediate-Autolayout and VFL, autolayoutvfl
*/The following are not commonly used
// Be sure to remember // 1. when setting constraints for a control, you must disable autoresing on the control. // 2. when adding constraints to a control, make sure that the control is in the hierarchy of the controller View.
-(Void) viewDidLoad {
[Super viewDidLoad];
UIView * redView = [[UIView alloc] init];
RedView. backgroundColor = [UIColor redColor];
[Self. view addSubview: redView];
UIView * blueView = [[UIView alloc] init];
BlueView. backgroundColor = [UIColor blueColor];
[Self. view addSubview: blueView];
// Constraint Constraints
/*
The first parameter Item
1. controls to be constrained
Attributes of the second parameter Constraint
NSLayoutAttributeLeft = 1, left
NSLayoutAttributeRight, right
NSLayoutAttributeTop, top
NSLayoutAttributeBottom, bottom
NSLayoutAttributeLeading, left
NSLayoutAttributeTrailing, right
NSLayoutAttributeWidth, width
NSLayoutAttributeHeight, high
NSLayoutAttributeCenterX, horizontal midline
NSLayoutAttributeCenterY, vertical midline
The third parameter is the link.
Typedef NS_ENUM (NSInteger, NSLayoutRelation ){
NSLayoutRelationLessThanOrEqual =-1, "<="
NSLayoutRelationEqual = 0, "="
NSLayoutRelationGreaterThanOrEqual = 1, "> ="
};
Fourth parameter reference object (reference Control)
The fifth parameter references the properties of the control.
Multiplier Multiplication
Constant +
Formula
Item1.attribute <relation> item2.attribute * multiplier + constant
*/
// Remember
// 1. When setting constraints for a control, you must disable autoresing on the control.
BlueView. translatesAutoresizingMaskIntoConstraints = NO;
RedView. translatesAutoresizingMaskIntoConstraints = NO;
// 2. When adding constraints to a control, make sure that the control is in the hierarchy of the controller View.
// Add the Blue View top
NSLayoutConstraint * blueTop = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem: self. view attribute: Invalid multiplier: 1 constant: 20]
[Self. view addConstraint: blueTop];
// Add the Blue View to the left
NSLayoutConstraint * blueLeft = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeLeft relatedBy: NSLayoutRelationEqual toItem: self. view attribute: Invalid multiplier: 1 constant: 20]
[Self. view addConstraint: blueLeft];
// Add the right side of the Blue View
NSLayoutConstraint * blueRight = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeRight relatedBy: Too toItem: self. view attribute: Too multiplier: 1 constant:-20];
[Self. view addConstraint: blueRight];
// Add the height of the Blue View
NSLayoutConstraint * blueheimpaired = [NSLayoutConstraint constraintWithItem: blueView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: Invalid multiplier: 1 constant: 100]
[Self. view addConstraint: blueheimpaired];
// Add the constraints on the red view
// Red top
NSLayoutConstraint * redTop = [NSLayoutConstraint constraintWithItem: redView attribute: NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem: blueView attribute: extends multiplier: 1 constant: 20];
[Self. view addConstraint: redTop];
// Align the red view with the right of the Blue view
NSLayoutConstraint * redRight = [NSLayoutConstraint constraintWithItem: redView attribute: NSLayoutAttributeRight relatedBy: Too toItem: blueView attribute: NSLayoutAttributeRight multiplier: 1 constant: 0];
[Self. view addConstraint: redRight];
// The height of the red view is the same as that of the Blue view.
NSLayoutConstraint * redHeight = [NSLayoutConstraint constraintWithItem: redView attribute: NSLayoutAttributeHeight relatedBy: Too toItem: blueView attribute: NSLayoutAttributeHeight multiplier: 1 constant: 0];
[Self. view addConstraint: redHeight];
// The height of the red view is the same as that of the Blue view.
NSLayoutConstraint * redWidth = [NSLayoutConstraint constraintWithItem: redView attribute: nslayoutbutewidth relatedBy: Too toItem: blueView attribute: NSLayoutAttributeWidth multiplier: 0.5 constant: 0];
[Self. view addConstraint: redWidth];
}
Animation self. vSpaceConstraint. constant + = 100; self. hSpaceContraint. constant + = 100;
Self. widthcontraint. constant ++ = 100;
Self. heightContraint. constant ++ = 100;
[UIView animateWithDuration: 1 animations: ^ {
// This method is used to re-layout the interface
// Calculate the constraint and then adjust the position of the control
[Self. view layoutIfNeeded];}];
-- Vfl syntax is occasionally used // You must disable autoresizing-(void) viewDidLoad {
[Super viewDidLoad];
// 1. Create a child control and add it to the Controller view.
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];
// 2. Disable autoresizing
BlueView. translatesAutoresizingMaskIntoConstraints = NO;
RedView. translatesAutoresizingMaskIntoConstraints = NO;
// 3. Add constraints through VFL
// Options alignment
// Horizontal direction
NSArray * hConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: |-20-[blueView]-20-|" options: metrics: nil views :@{@ "blueView ": blueView}];
[Self. view addConstraints: hConstraints];
// Vertical
NSArray * vConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @ "V: |-20-[blueView (100)]-20-[redView (= blueView)]" options: Alert metrics: nil views: @ {@ "blueView": blueView, @ "redView": redView}];
[Self. view addConstraints: vConstraints];
// VFL cannot be involved in Calculation
// NSArray * h1Constraints = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: [redView (= blueView * 0.5)]" options: Using metrics: nil views :@{ @ "blueView ": blueView, @ "redView": redView}];
//
// [Self. view addConstraints: h1Constraints];
NSLayoutConstraint * redW = [NSLayoutConstraint constraintWithItem: redView attribute: NSLayoutAttributeWidth relatedBy: Too toItem: blueView attribute: NSLayoutAttributeWidth multiplier: 0.5 constant: 0];
[Self. view addConstraint: redW];
}