zoukankan      html  css  js  c++  java
  • 屏幕适配/autoLayout autoresizingMask

    #pragma mark-- 屏幕适配/autoLayout autoresizingMask

    1> 发展历程
    代码计算frame -> autoreszing(父控件和子控件的关系) -> autolayout(任何控件都可以产生关系) -> sizeclass


    2> sizeclass
    * 仅仅是对屏幕进行了分类, 真正排布UI元素还得使用autolayout
    * 不再有横竖屏的概念, 只有屏幕尺寸的概念
    * 不再有具体尺寸的概念, 只有抽象尺寸的概念
    * 把宽度和高度各分为3种情况
    1) Compact : 紧凑(小)
    2) Any : 任意
    3) Regular : 宽松(大)
    4) 符号代表
    - : Compact
    * : Any
    + : Regular
    5) 继承性
    * * : 其它8种情况都会继承
    * - : 会被- - + -继承
    + * : 会被+ - + +继承
    6) sizeclass和autolayout的作用
    sizeclass:仅仅是对屏幕进行了分类
    autolayout:对屏幕中各种元素进行约束(位置尺寸)


    方法一:
    // self.aView 的上面(X) = self.view 的上面 (X) * 1 + 20
    NSLayoutConstraint * aViewTop = [NSLayoutConstraint constraintWithItem:self.aView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:20];

    //优先级 默认为1000 最大为1000
    imageViewWidth.priority = 1000;

    //压缩扩张的优先级默认为750 0 :水平方向 1:竖直方向
    [imageV setContentCompressionResistancePriority:1000 forAxis:0];

    //拥抱的优先级默认为250 0 :水平方向 1:竖直方向
    [imageViewWidth setContentHuggingPriority:800 forAxis:0];

    方法二:字符串(需要拼写正确)
    NSString * str_H = @"H:|-x-[aView(200)]-20-[bView(==aView)]";

    //H:水平方向 V:竖直方向
    // | 表示边界
    // - 表示距离某个边界或者组件。x 表示距边界的距离,如果组件和组件,组件和边界之间没有 - ,则表示两者之间的距离是0.
    // []表示的是组件 ,()里的数值表示某个方向上的宽度

    NSString * str_V1 = @"V:|-50-[aView(y)]";
    NSString * str_V2 = @"V:|-50-[bView(200)]";

    NSDictionary * dic = NSDictionaryOfVariableBindings(aView,bView);

    //metrics里面的字典是除了view之外的参数的数值(如果有的话)
    NSArray * c_H = [NSLayoutConstraint constraintsWithVisualFormat:str_H options:0 metrics:@{@"x":@10} views:dic];
    [self.view addConstraints:c_H];

    NSArray * c_V1 = [NSLayoutConstraint constraintsWithVisualFormat:str_V1 options:0 metrics:@{@"y":@100} views:dic];
    [self.view addConstraints:c_V1];

    单行的label布局 x:左 y:上 height width距离右边界 大于等于
    多行label布局 下面的组件不固定的时候 x:左 y:上
    height优先级降低750
    竖直方向的正压力优先级增大(当label的字数增加时高度增加)
    竖直方向的负压力的优先级增大(当label的字体比较小时会减小到比height还小)
    width距离右边界 大于等于


    九张图片的布局
    1:x y
    1/2/3 top 等高等宽 宽高比1:1
    2:x
    3:x 右边界
    1/4/7 leading 等宽等高
    4:y
    7:y 下边界
    4:高度 小于等于 (高度优先级200)
    7:高度 小于等于 (高度优先级200)
    5:x y
    6:x y (右边界)
    4/5/6:等宽登高
    8:x y (下)
    9:x y (右边界)(下)
    7/8/9:等宽登高
    有一个距离下的就可以把cell撑起来 (右边界)


    使用第三方布局Masonry
    // 蓝色控件
    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]; //一定要先添加到视图上再添加约束否则会崩溃

    // 添加约束 
    CGFloat margin = 20;
    CGFloat height = 50;
    [blueView makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(margin);
    //equalTo可以设置偏移量make.width.mas_equalTo(60);直接设置大小
    make.right.equalTo(redView.left).offset(-margin);
    make.bottom.equalTo(self.view.bottom).offset(-margin);
    make.height.equalTo(height);
    make.top.equalTo(redView.top);
    make.bottom.equalTo(redView.bottom);
    make.width.equalTo(redView.width);
    }];

    [redView makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view.right).offset(-margin);
    }];

     更多内容--> 博客导航 每周一篇哟!!!


    有任何关于iOS开发的问题!欢迎下方留言!!!或者邮件lieryangios@126.com 虽然我不一定能够解答出来,但是我会请教iOS开发高手!!!解答您的问题!!!

  • 相关阅读:
    经典网络复现(0)多层感知机和lenet
    将自有数据集下yolov训练结果(*.weights) 在进行部署
    DL基础学习计划
    自有数据集上使用keras训练YOLOv3目标检测
    《基于深度学习的图像语义分割方法综述》阅读理解
    Hessian矩阵以及在血管增强中的应用—OpenCV实现
    QML官方例子Calculator初步解析
    human_pose_estimation_demo的再进一步研究(基于OpenPOSE)
    10年图像处理工程师学习图像处理的小结
    human_pose_estimation_demo的进一步研究(基于OpenPOSE)
  • 原文地址:https://www.cnblogs.com/CoderEYLee/p/Object-C-0019.html
Copyright © 2011-2022 走看看