iOS笔记3

  1. 适配的历史
    4s 以前 不需要适配
    坐标都是写死的
    [uiScreen mainScreen ].bounds.size
    UIButton *btn = nil;
    btn.frame = CGRectMake(20,20,320,480)
    ios 4
    autoResizing 做屏幕适配

    ios 6
    autoLayout 使用最广泛的屏幕适配

    ios 8
    sizeClass 最新的屏幕适配

  2. autoResizing
    注意点 不能跟autoLayout 共存
    高度/宽度 跟着父控件的高度/宽度进行缩放
    四根线 固定哪个位置 一般两根线就能确定一个位置

    局限性 兄弟控件不能设置间距 只能是相对于父控件

    使用代码 实现autoResizing 控件.autoresizingMask
    // 根storyboard中相反的 storyboard中是 固定那个方向 代码是拉伸那个方向

    //UIViewAutoresizingNone就是不自动调整。
    //UIViewAutoresizingFlexibleLeftMargin 自动调整与superView左边的距离,保证与superView右边的距离不变。
    //UIViewAutoresizingFlexibleRightMargin 自动调整与superView的右边距离,保证与superView左边的距离不变。
    //UIViewAutoresizingFlexibleTopMargin 自动调整与superView顶部的距离,保证与superView底部的距离不变。
    //UIViewAutoresizingFlexibleBottomMargin 自动调整与superView底部的距离,也就是说,与superView顶部的距离不变。
    //UIViewAutoresizingFlexibleWidth 自动调整自己的宽度,保证与superView左边和右边的距离不变。
    //UIViewAutoresizingFlexibleHeight 自动调整自己的高度,保证与superView顶部和底部的距离不变。
    //UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin 自动调整与superView左边的距离,保证与左边的距离和右边的距离和原来距左边和右边的距离的比例不变。比如原来距离为20,30,调整后的距离应为68,102,即68/20=102/30。
    //
    //其它的组合类似。

    具体代码的实现
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor yellowColor];
        view.frame  = CGRectMake(self.view.frame.size.width - 50, self.view.frame.size.height - 50, 50, 50);
        [self.view addSubview:view];
        view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
  3. autoLayout:
    概念
    1. 参照
    相对哪个控件设置约束
    哪个控件离当前空间最近,就参照哪个控件
    2. 约束
    尺寸
    宽高
    位置
    xy
    trailing 右边
    leading 左边

    思路
    1. 如果是两个控件的话 那么先搞定一个

  4. autoLayout使用代码布局
    autoLayout 与 autoResizing 不能共存
    view.translatesAutoresizingMaskIntoConstraints = NO;
    使用NSLayoutConstrain 调用 constrainWithItem
    第一个参数 需要约束的控件
    第二个参数 控件的属性
    第三个参数 等于 / <= / 大于等于
    第四个参数 相对那个控件的约束
    第五个参数 相对哪个控件的属性
    第六个参数 multiplier 乘以��
    第七个参数 常量 需要加还是减

    注意点 以上操作哪个控件最大 就添加到哪个控件上,如果是兄弟那么就添加到老爸身上 , 同一个爷爷的不同的爸爸 添加到爷爷身上
    “添加约束到最大的控件上”

  5. vfl 布局子控件
    VFL全称是Visual Format Language,翻译过来是“可视化格式语言
    抽象语言
    知道是神马东西就可以了
    H:|(控制器左边)-20(间距)-redView-20(间距)-[blueView(显示的控件)(==redView(控件的宽度等于红色控件的宽度))]-20-|(控制器的右边)

    使用 NSLayoutConstrain constraintsWithVisualFormat
    第一个参数 水平或者是垂直方向的约束(字符串)
    第二个参数 对齐的选项
    第三个参数 使用到数字可以添加到字典中
    第四个参数 将所有的控件的添加到字典中, 键值对 名字都是一样的

    “添加数组到self.view”

  6. masonry使用
    使用步骤
    1. 导入masonry的头文件
    2. A控件需要约束
    使用A 调用 mas_makeConstrain
    在block中设置约束
    make 就相当于 调用者(控件)
    3. mas_equalTo和equalTo
    默认情况下
    mas_equalTo有自动包装功能,比如自动将20包装为@20
    equalTo没有自动包装功能

        如果添加了下面的宏,那么mas_equalTo和equalTo就没有区别
        #define MAS_SHORTHAND_GLOBALS
        // 注意:这个宏一定要添加到#import "Masonry.h"前面
    4. mas_width和width
        默认情况下
        width是make对象的一个属性,用来添加宽度约束用的,表示对宽度进行约束
        mas_width是一个属性值,用来当做equalTo的参数,表示某个控件的宽度属性
    
        如果添加了下面的宏,mas_width也可以写成width
        #define MAS_SHORTHAND
    
        mas_height、mas_centerX以此类推
    
  7. 优先级 & 动画
    优先级最高是的1000
    最低的是0
    只要是优先级小于1000的 那么就第二次执行(当最高优先级不存在)

    动画
    当界面发生改变的时候 如果需要动画重新布局
    [self.view layoutIfNeeded]

  8. Autolayout的警告和错误
    警告
    控件的frame不匹配所添加的约束, 比如
    比如约束控件的宽度为100, 而控件现在的宽度是110

    错误
    缺乏必要的约束, 比如
    只约束了宽度和高度, 没有约束具体的位置

    两个约束冲突, 比如
    1个约束控件的宽度为100, 1个约束控件的宽度为110

  9. 自动布局的核心计算公式
    obj1.property1 =(obj2.property2 * multiplier)+ constant value

  10. 添加约束的规则
    1)对于两个同层级view之间的约束关系,添加到它们的父view上
    2)对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上
    3)对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上

  11. VFL语言
    什么是VFL语言
    VFL全称是Visual Format Language,翻译过来是“可视化格式语言”
    VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言

  12. VFL示例
    H:[cancelButton(72)]-12-[acceptButton(50)]
    canelButton宽72,acceptButton宽50,它们之间间距12

    H:[wideView(>=60@700)]
    wideView宽度大于等于60point,该约束条件优先级为700(优先级最大值为1000,优先级越高的约束越先被满足)

    V:[redBox][yellowBox(==redBox)]
    竖直方向上,先有一个redBox,其下方紧接一个高度等于redBox高度的yellowBox

    H:|-10-[Find]-[FindNext]-[FindaField(>=20)]-|
    水平方向上,Find距离父view左边缘默认间隔宽度,之后是FindNext距离Find间隔默认宽度;再之后是宽度不小于20的FindField,它和FindNext以及父view右边缘的间距都是默认宽度。(竖线“|” 表示superview的边缘)

  13. VFL的使用
    使用VFL来创建约束数组

    • (NSArray )constraintsWithVisualFormat:(NSString )format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary )metrics views:(NSDictionary )views;
      format :VFL语句
      opts :约束类型
      metrics :VFL语句中用到的具体数值
      views :VFL语句中用到的控件

    创建一个字典(内部包含VFL语句中用到的控件)的快捷宏定义
    NSDictionaryOfVariableBindings(…)

  14. 基于Autolayout的动画
    在修改了约束之后,只要执行下面代码,就能做动画效果
    [UIView animateWithDuration:1.0 animations:^{
    [添加了约束的view layoutIfNeeded];
    }];

  15. autoLayout代码实现
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor ];
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];

    NSLayoutConstraint *topLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTopMargin relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTopMargin multiplier:1.0f constant:20];
    [self.view addConstraint:topLc];
    
    
    NSLayoutConstraint *leftLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeftMargin multiplier:1.0 constant:20];
    [self.view addConstraint:leftLc];
    
    NSLayoutConstraint *withLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
    [redView addConstraint:withLc];
    
    NSLayoutConstraint *heightLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:100];
    [redView addConstraint:heightLc];
    
  16. VFL代码的使用
    //…………有待研究

    UIView *redView = [[UIView alloc]init];
    [self.view addSubview:redView];

    UIView *blueView = [[UIView alloc]init];
    [self.view addSubview:blueView];

    blueView.backgroundColor = [UIColor blueColor];
    redView.backgroundColor = [UIColor redColor];

    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    redView.translatesAutoresizingMaskIntoConstraints = NO;

    // vfl
    // |(控制器左边)-20(间距)-redView-20(间距)-[blueView(显示的控件)(==redView(控件的宽度等于红色控件的宽度))]-20-|(控制器的右边)
    NSString *hStr = @”H:|-margin-[redView]-margin-[blueView(==redView)]-margin-|”;
    NSString *vStr = @”V:[redView(height)]-margin-|”;

    // NSDictionary *views = @{
    // @”redView” : redView,
    // @”blueView”: blueView
    // };
    // 直接传控件对象就可以了 自动生成字符串
    NSDictionary *views = NSDictionaryOfVariableBindings(redView,blueView);

    NSDictionary *metrics = @{
    @”margin” : @20,
    @”height” : @40
    };

    NSArray *hLcArray = [NSLayoutConstraint constraintsWithVisualFormat:hStr options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views];
    [self.view addConstraints:hLcArray];

    NSArray *vLcArray = [NSLayoutConstraint constraintsWithVisualFormat:vStr options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views];
    [self.view addConstraints:vLcArray];

  17. Masonry代码具体使用

    • (void)viewDidLoad {
      [super viewDidLoad];

      // 创建蓝色控件
      UIView *blueView = [[UIView alloc] init];
      blueView.backgroundColor = [UIColor blueColor];
      [self.view addSubview:blueView];

      [blueView makeConstraints:^(MASConstraintMaker *make) {
      make.width.height.equalTo(100);
      make.centerX.centerY.equalTo(self.view);
      }];
      }

    • (void)doubleViews
      {
      // 创建蓝色控件
      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];
      // 创建约束(只会添加新的约束)
      [blueView makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(self.view.left).offset(20);
      make.bottom.equalTo(self.view.bottom).with.offset(-20);
      make.right.equalTo(redView.left).with.offset(-20);
      make.height.equalTo(40);
      make.width.equalTo(redView.width);
      }];

      [redView makeConstraints:^(MASConstraintMaker *make) {
      make.right.equalTo(self.view.mas_right).offset(-20);
      make.top.equalTo(blueView.mas_top);
      make.bottom.equalTo(blueView.mas_bottom);
      }];

      // 更新约束
      [blueView updateConstraints:^(MASConstraintMaker *make) {
      make.height.equalTo(80);
      }];

      // 删除之前的约束,重新添加约束
      // [blueView remakeConstraints:^(MASConstraintMaker *make) {
      //
      // }];
      }

    • (void)center
      {
      // 创建蓝色控件
      UIView *blueView = [[UIView alloc] init];
      blueView.backgroundColor = [UIColor blueColor];
      [self.view addSubview:blueView];

      // 添加新的约束
      // [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      // make.width.equalTo(@100);
      // make.height.equalTo(@100);
      // make.centerX.equalTo(self.view.mas_centerX);
      // make.centerY.equalTo(self.view.mas_centerY);
      // }];
      // [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      // make.width.mas_equalTo(@100);
      // make.height.mas_equalTo(@100);
      // make.centerX.mas_equalTo(self.view);
      // make.centerY.mas_equalTo(self.view);
      // }];
      // [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      // make.width.mas_equalTo(100);
      // make.height.mas_equalTo(100);
      // make.centerX.equalTo(self.view);
      // make.centerY.equalTo(self.view);
      // }];
      [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.size.mas_equalTo(CGSizeMake(100, 100));
      make.center.mas_equalTo(self.view);
      }];
      }

    • (void)edge
      {
      // 创建蓝色控件
      UIView *blueView = [[UIView alloc] init];
      blueView.backgroundColor = [UIColor blueColor];
      [self.view addSubview:blueView];

      // 添加新的约束
      // [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      // make.left.equalTo(self.view.mas_left).offset(20);
      // make.top.equalTo(self.view.mas_top).offset(20);
      // make.right.equalTo(self.view.mas_right).offset(-20);
      // make.bottom.equalTo(self.view.mas_bottom).offset(-20);
      // }];
      // [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(self.view).offset(20);
      make.top.equalTo(self.view).offset(20);
      //
      // make.left.top.equalTo(self.view).offset(20);
      //
      // make.right.equalTo(self.view).offset(-20);
      // make.bottom.equalTo(self.view).offset(-20);
      // }];
      // [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      // make.left.offset(20);
      // make.top.offset(20);
      // make.right.offset(-20);
      // make.bottom.offset(-20);
      // }];
      [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(20, 20, 20, 20));
      // make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(30, 50, 40, 20));
      }];
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值