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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容