iOS--适配

#import "LayoutConstraintVC.h"

#import "Masonry.h"

@interface LayoutConstraintVC (){

    NSLayoutConstraint*leftLayoutConstraint;

    UIView* yellowview;

}

@property (nonatomic, strong) MASConstraint *topConstraint;//通过将约束make表达式的结果分配给局部变量或类属性,可以保留特定约束的引用。您还可以通过将多个约束存储在数组中来引用它们。

@end

@implementationLayoutConstraintVC

- (void)viewDidLoad {

    [super viewDidLoad];

    [self initInterface];

    [selfaddBut];

  // [self LayoutConstraint1];

   // [self LayoutConstraint2];

    [self initMasonry];

}

#pragma mark ---添加按钮

-(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];//移除约束

      [self.topConstraint install];//安装约束

    /*

     mas_updateconstr对于更新一组约束非常有用,但是除了更新常量值之外的任何操作都可能使您精疲力尽。这就是mas_remakeconstr的作用。

     mas_remakeconstr类似于mas_updateconstr,但它将在重新安装之前删除所有约束,而不是更新常量值。这允许您提供不同的约束,而不必保留对希望删除的约束的引用。

     */

    //更新约束

//    [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);//视图的左边等于父视图的左边

        // make.right.mas_equalTo(-20);//等于父视图的右边-20

        make.right.mas_equalTo(-20).with.offset(1);//视图的右边等于父视图的右边----with是一个可选的语义填充

        make.height.mas_equalTo(100);//视图的高度为100

    }];

    /*

     leftLayoutConstraint.constant = 50;//与其他属性不同,常量可以在约束创建之后修改。在现有的约束上设置常量要比删除约束并添加一个与旧约束类似的新约束,但是需要一个新的常量,这样做的效果要好得多。

     [yellowview updateConstraints];//更新约束

     */

}

#pragma mark--界面分割

- (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进行分装的第三方框架,对应的swift版本为Snapkit

     */

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    yellowview.translatesAutoresizingMaskIntoConstraints = NO;

    //重点:进行布局的时候,必需是在[addSubview]以后

    [yellowview mas_makeConstraints:^(MASConstraintMaker *make) {

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

        //make.left.mas_equalTo(20);//距离父视图的左边20

        make.left.mas_equalTo(self.view.mas_left);//视图的左边等于父视图的左边

       // make.right.mas_equalTo(-20);//等于父视图的右边-20

        make.right.mas_equalTo(-20).with.offset(1);//视图的右边等于父视图的右边----with是一个可选的语义填充

        make.height.mas_equalTo(100);//视图的高度为100

        //make.size.mas_equalTo(CGSizeMake(50, 100));视图的大小

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

    }];

    [yellowview setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];//setContentCompressionResistancePriority:代表的是被压缩的权限,越高越不容易被压缩)

    //有时,您需要修改现有的约束,以使其具有动画效果或删除/替换约束。

}

#pragma mark---界面适配1--AutoLayout-NSLayoutConstraint---2

-(void)LayoutConstraint2{

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    /*

     重点 使用NSLayoutConstraint之前,我们需要确定一点:为了防止constraint和view本身的autoresizing属性冲突,我们需要设置view的属性:

     */

    yellowview.translatesAutoresizingMaskIntoConstraints = NO;

    // 设置约束----使用类似ASCII艺术的可视格式字符串创建约束数组。

    NSDictionary *dict = NSDictionaryOfVariableBindings(yellowview);//这个宏是一个帮助工具,用于为+constraintsWithVisualFormat:options:metrics:views:创建视图字典。变量绑定(v1, v2, v3)等价于[NSDictionary dictionaryWithObjectsAndKeys:v1, @“v1”,v2, @“v2”,v3, @“v3”,nil];

    /*

     vf0---指定Contsraint的属性,是垂直方向的限定还是水平方向的限定,参数定义一般如下:

     V:|-(>=XXX) :表示垂直方向上相对于SuperView大于、等于、小于某个距离

     若是要定义水平方向,则将V:改成H:即可

    在接着后面-[]中括号里面对当前的View/控件 的高度/宽度进行设定;

     options:字典类型的值;这里的值一般在系统定义的一个enum里面选取

     metrics:nil;一般为nil ,参数类型为NSDictionary,从外部传入 //衡量标准

     views:就是上面所加入到NSDictionary中的绑定的View

     1.|-[view]-|:  视图处在父视图的左右边缘内

     2.|-[view]  :  视图处在父视图的左边缘

     3.|[view]  :  视图和父视图左边对齐

     4.-[view]-  :  设置视图的宽度高度

     |: 表示父视图

     -:表示距离

     V:  :表示垂直

     H:  :表示水平

     >= :表示视图间距、宽度和高度必须大于或等于某个值

     <= :表示视图间距、宽度和高度必须小宇或等于某个值

     == :表示视图间距、宽度或者高度必须等于某个值

     */

    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---界面适配1-AutoLayout--NSLayoutConstraint---1

-(void)LayoutConstraint1{

    yellowview = [[UIView alloc]init];

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    /*

    重点 使用NSLayoutConstraint之前,我们需要确定一点:为了防止constraint和view本身的autoresizing属性冲突,我们需要设置view的属性:

     */

    yellowview.translatesAutoresizingMaskIntoConstraints = NO;

    /*

     设置约束

     @param view1 指定需要添加约束的视图一

     @param attr1 指定视图一需要约束的属性

     @param relation 指定视图一和视图二添加约束的关系

     @param view2 指定视图一依赖关系的视图二;可为nil

     @param attr2 指定视图一所依赖的视图二的属性,若view2=nil,该属性设置 NSLayoutAttributeNotAnAttribute

     @param multiplier 系数  情况一和二为亲测

     情况一:设置A视图的高度 = A视图高度 * multiplier + constant;此时才会起作用;

     情况二:设置A视图和其他视图的关系或 toItem=nil,multiplier设置不等于0即可,若等于0会crash;

     @param c 常量

     @return 返回生成的约束对象

     [所添加的视图 addConstraint:返回生成的约束对象];

     如果---view2 为nil,则约束条件可以直接添加到view1,也可以添加到view1的父视图

     如果---view2 不为nil,当view1、view2视图级别相同时,则添加到他们共同的父视图

                        如果一个为另一个的父视图,如view1是view2的父视图,则需要添加到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]];//

     //设置顶部

     [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---界面初始化

- (void)initInterface{


    self.title = @"LayoutConstraint";

    self.view.backgroundColor = [UIColor whiteColor];

}

@end

猜你喜欢

转载自blog.csdn.net/weixin_33725722/article/details/90919662