iOS-- adaptation

#import "LayoutConstraintVC.h"

#import "Masonry.h"

@interface LayoutConstraintVC (){

    NSLayoutConstraint * leftLayoutConstraint;

    UIView* yellowview;

}

@property (nonatomic, strong) MASConstraint * topConstraint; // make constrained by the result of the expression is assigned to a local variable or class attribute references may retain certain constraints. You can also store multiple constraints to refer to them in an array.

@end

@implementationLayoutConstraintVC

- (void)viewDidLoad {

    [super viewDidLoad];

    [self initInterface];

    [selfaddBut];

  // [self LayoutConstraint1];

   // [self LayoutConstraint2];

    [self initMasonry];

}

#pragma mark --- Add button

-(void)addBut{

    UIButton* but = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.view addSubview:but];

    but.backgroundColor = [UIColor redColor];

    but.frame = CGRectMake(80, self.view.frame.size.height/2.0+84, 80, 30);

    [butaddTarget:self action:@selector(but) forControlEvents:UIControlEventTouchUpInside];

}

-(void)but{

     [Self.topConstraint uninstall]; // constraint is removed

      [Self.topConstraint install]; // installation constraints

    /*

     mas_updateconstr very useful for updating a set of constraints, but any operations other than the constant value updates can make you exhausted. This is the role of mas_remakeconstr.

     mas_remakeconstr similar mas_updateconstr, but it removes all constraints before reinstalling, rather than updating a constant value. This allows you to provide different constraints, without reservation to want to remove the constraint references.

     */

    // update constraint

//    [yellowview mas_updateConstraints:^(MASConstraintMaker *make) {

//        make.top.mas_equalTo(120);

//    }];

    [yellowview mas_remakeConstraints:^(MASConstraintMaker *make) {

         make.top.mas_equalTo(120);

        make.left.mas_equalTo (self.view.mas_left); // left side view of the left side of the parent view is equal to

        // make.right.mas_equalTo (-20); // is equal to the right of the parent view -20

        make.right.mas_equalTo (-20) .with.offset (1); // right side view of the right side is equal with the parent view ---- is an optional semantic filling

        (100) make.height.mas_equalTo; height // view 100

    }];

    /*

     leftLayoutConstraint.constant = 50; // Unlike other properties, constants can be modified after the constraints created. Set constant in the existing constraints than delete the constraint and add a new constraint similar to the old constraints, but requires a new constant, doing so much better.

     [Yellowview updateConstraints]; // update constraint

     */

}

#pragma mark-- interface division

- (void)initFrame{

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    yellowview.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+84, self.view.bounds.size.width, self.view.bounds.size.height*0.2);

}

#pragma mark-----Masonry

-(void)initMasonry{

    /*

     Masonry, Auto Layout is a third-party framework to be dispensed, the corresponding swift version Snapkit

     */

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    yellowview.translatesAutoresizingMaskIntoConstraints = NO;

    // Key: When the layout, it is necessary in the [addSubview] after

    [yellowview mas_makeConstraints:^(MASConstraintMaker *make) {

      self.topConstraint=  make.top.mas_equalTo(84);

        //Make.left.mas_equalTo(20);// left from the parent view 20

        make.left.mas_equalTo (self.view.mas_left); // left side view of the left side of the parent view is equal to

       // make.right.mas_equalTo (-20); // is equal to the right of the parent view -20

        make.right.mas_equalTo (-20) .with.offset (1); // right side view of the right side is equal with the parent view ---- is an optional semantic filling

        (100) make.height.mas_equalTo; height // view 100

        //make.size.mas_equalTo(CGSizeMake(50, 100)); view size

        //make.width.lessThanOrEqualTo(@400);//width <= 400

    }];

    [Yellowview setContentHuggingPriority: UILayoutPriorityRequired forAxis: UILayoutConstraintAxisHorizontal]; // setContentCompressionResistancePriority: represents the compressed authority, the more difficult the higher the compression)

    // Sometimes you need to modify the existing constraints, so animated or delete / replace constraints.

}

#pragma mark --- Interface Adapter 1 - AutoLayout-NSLayoutConstraint --- 2

-(void)LayoutConstraint2{

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    /*

     Prior to focus on the use of NSLayoutConstraint, we need to determine one thing: To prevent constraint and view themselves autoresizing property conflicts, we need to set the properties view of:

     */

    yellowview.translatesAutoresizingMaskIntoConstraints = NO;

    // create a constraint set constraint ---- string array using a visual format similar to ASCII art.

    NSDictionary * dict = NSDictionaryOfVariableBindings (yellowview); // This macro is a help tool, used to + constraintsWithVisualFormat: options: metrics: views: create a view dictionary. Variable bindings (v1, v2, v3) is equivalent to [NSDictionary dictionaryWithObjectsAndKeys: v1, @ "v1", v2, @ "v2", v3, @ "v3", nil];

    /*

     --- Vf0 Contsraint specified attribute is defined in the horizontal direction or defining a vertical direction, the general parameter is defined as follows:

     V: | - (> = XXX): with respect to a vertical direction SuperView greater than, equal to, less than a certain distance

     If you want to define the horizontal direction, then V: changed to H: you can

    View the current / control height / width is set in [] brackets - in a subsequent later;

     options: a dictionary of values; generally selected value here defined in a system which enum

     metrics: nil; generally nil, parameters of type NSDictionary, // measure passed in from the outside

     views: NSDictionary is added to the above binding in the View

     1. | - [view] - |: left and right edge view in the parent view

     2. | - [view]: View at the left edge of the parent view

     3. | [view]: the left view and the parent view alignment

     4 .- [view] -: Set Width Height view

     |: Represents the parent view

     -: indicates the distance

     V:: represents a vertical

     H:: represents the horizontal

     > =: A view showing the pitch, width and height must be greater than or equal to a certain value

     <=: A view showing the pitch, width and height must be a value equal to or Xiaoyu

     ==: a view showing the pitch, width or height must be equal to a certain value

     */

    NSString *vf0 = @"|-20-[yellowview]-30-|";

    NSString *vf1 = [NSString stringWithFormat:@"V:|-84-[yellowview(%f)]",100.0];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vf0 options:0 metrics:nil views:dict]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vf1 options:0 metrics:nil views:dict]];

}

#pragma mark --- Interface Adapter 1-AutoLayout - NSLayoutConstraint --- 1

-(void)LayoutConstraint1{

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    /*

    Prior to focus on the use of NSLayoutConstraint, we need to determine one thing: To prevent constraint and view themselves autoresizing property conflicts, we need to set the properties view of:

     */

    yellowview.translatesAutoresizingMaskIntoConstraints = NO;

    /*

     Set constraints

     @Param view1 view a specified constraints need to be added

     attr1 constraints require a specific view property @param

     @param relation specified view and a view of the relationship between two add constraints

     @param view2 view of a designated view of two dependencies; may be nil

     attr2 specified view @param a two view-dependent properties, if view2 = nil, the property is set NSLayoutAttributeNotAnAttribute

     @param multiplier coefficients and two for the case of a pro-test

     Case 1: A view of the height setting view height = A * multiplier + constant; at this time will work;

     Case 2: A relationship is provided and other views or toItem = nil, multiplier is not set equal to 0, will be equal to 0 if Crash;

     @param c constants

     @return returns constraint objects generated

     [Added view addConstraint: Returns the constraint objects generated];

     If --- View2 to nil, the constraints can be added directly to the view1, may be added to the parent view view1

     --- view2 if not nil, when view1, view2 view level are the same, adding to their common parent view

                        If one of the other parent view, the parent view as view2 view1 is the need to add to view1

     */

     leftLayoutConstraint=  [NSLayoutConstraint constraintWithItem:yellowview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:+50.0];

     [self.view addConstraint:leftLayoutConstraint];//左边

     [self.view addConstraint: [NSLayoutConstraint constraintWithItem:yellowview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-50.0]];//

     // set the top

     [self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:84.0]];

    // [self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0.0]];

  [yellowview addConstraint:[NSLayoutConstraint constraintWithItem:yellowview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:100.0]];

    NSLog(@"%@",yellowview);//UIView: 0x7fc4144207e0; frame = (0 0; 0 0); layer =

      NSLog(@"%@",yellowview.layer);

     NSLog(@"%f",yellowview.layer.frame.size.height);

     NSLog(@"%f",yellowview.center.y);

    NSLog(@"%@",yellowview.widthAnchor);

     NSLog(@"%f",yellowview.bounds.size.height);

      NSLog(@"%f",yellowview.frame.size.width);

    NSLog(@"%f",yellowview.frame.size.height);

    NSLog(@"%f",  yellowview.layer.frame.size.height);

}

#pragma mark --- initialization interface

- (void)initInterface{


    self.title = @"LayoutConstraint";

    self.view.backgroundColor = [UIColor whiteColor];

}

@end

Guess you like

Origin blog.csdn.net/weixin_33725722/article/details/90919662