当前位置: 首页 > news >正文

iOS 之自动布局

      项目要做iPhone版和iPad的适配,就找了一些资料 关于iOS的自动布局,学习的一些收获以及心得给大家分享一下。
      xib的布局就不说了,就是线的连接,主要分享一下纯代码的一些自动布局的学习心得。
      Autolayout的强大是毋庸质疑的,当你熟悉了它之后,你肯定会觉得它很方便的实现布局,布局将会比使用frame的绝对坐标时还方便。
        UIView *superview = self;
UIView *view1 = [[UIView alloc] init];
        view1.translatesAutoresizingMaskIntoConstraints = NO;
        view1.backgroundColor = [UIColor greenColor];
        [superview addSubview:view1]; UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); [superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]];
最初,我在许多地方看到布局的时候,关于这句话一直不太理解 UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);之后找了一些资料,UIEdgeInsets 是控件的属性,由函数 UIEdgeInsetsMake ( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right );  构造出,分别表示其中的内容/标题/图片离各边的距离。
这只是其中一种写法,以下还有一个例子来讲解一下:

         UIView *redView = [[UIView alloc] init];

         redView.backgroundColor = [UIColor redColor];

         [self.view addSubview:redView];

         [redView setTranslatesAutoresizingMaskIntoConstraints:NO]; //标明将使用autolayout方式来布局

         NSMutableArray *tmpConstraints = [NSMutableArray array];

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

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

          若是要定义水平方向,则将V:改成H:即可;  在接着后面-[]中括号里面对当前的View/控件 的高度/宽度进行设定;

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

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

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

          在这里要注意的是 AddConstraints  和 AddConstraint 之间的区别,一个添加的参数是NSArray,一个是NSLayoutConstraint

       使用规则:

            |: 表示父视图

           -: 表示距离

           V:  :表示垂直

           H:  :表示水平

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

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

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

           @  :>=、<=、==  限制   最大为  1000

        具体事例:

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

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

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

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

          5.|-30.0-[view]-30.0-|:  表示离父视图 左右间距  30

          6.[view(200.0)] : 表示视图宽度为 200.0

          7.|-[view(view1)]-[view1]-| :表示视图宽度一样,并且在父视图左右边缘内

          8. V:|-[view(50.0)] : 视图高度为  50

          9: V:|-(==padding)-[imageView]->=0-[button]-(==padding)-| : 表示离父视图的距离为Padding,这两个视图间距必须大于或等于0并且距离底部父视图为 padding。

   10:  [wideView(>=60@700)]  :视图的宽度为至少为60 不能超过  700 ,最大为1000
     例子:

          NSArray *array0 = [NSLayoutConstraint constraintsWithVisualFormat:@"|-50-[redView(>=100)]" options:1 metrics:nil views:NSDictionaryOfVariableBindings(redView)];

          [tmpConstraints addObjectsFromArray:array0];

          NSArray *array1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[redView(==150)]" options:1 metrics:nil views:NSDictionaryOfVariableBindings(redView)];   

          [tmpConstraints addObjectsFromArray:array1];

        [self.view addConstraints:tmpConstraints];

          greenView = [[UIView alloc] init];

          greenView.backgroundColor = [UIColor greenColor];

        [self.view addSubview:greenView]; 
      [greenView setTranslatesAutoresizingMaskIntoConstraints:NO];
       NSMutableArray *tmpConstrains = [NSMutableArray array];
      [tmpConstrains addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView]-[greenView(>=100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(redView,greenView)]];
        [tmpConstrains addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[greenView(==150)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView)]];

           [self.view addConstraints:tmpConstrains];

           [self setTopLayout:[array1 objectAtIndex:0]];

         [self setLeftLayout:[array0 objectAtIndex:1]];

当然这种写的比较繁琐,也比较耗时,之后发现了一个第三方库,会使得自动布局变的简单 ,https://github.com/SnapKit/Masonry,具体使用就不详解了,里面有例子介绍。
 
 

 

 
 
      

转载于:https://www.cnblogs.com/amy54/p/5072564.html

相关文章:

  • Android四大基本组件(1)之Activity与BroadcastReceive广播接收器
  • css实现左侧固定宽,右侧自适应
  • Android四大基本组件(2)之Service 服务与Content Provider内容提供者
  • Android四大基本组件(3)之四大组件总结
  • Android 使用Gson解析json案例详解
  • 网络加载数据和解析JSON格式数据案例之空气质量监测应用
  • Android抽屉菜单DrawerLayout的实现案例
  • 进程注入的学习(中)
  • Android程序之全国天气预报查询(聚合数据开发)
  • 什么时候用webserver,什么时候用一般处理文件
  • Android注册短信验证码功能
  • Android程序解析XML文件的方法及使用PULL解析XML案例
  • 西安Uber优步司机奖励政策(12月21日-12.27日)
  • Android案例之新闻客户端服务器实现,完全属于自己的新闻展示平台
  • Ubuntu下hadoop2.4搭建集群(单机模式)
  • @jsonView过滤属性
  • [译]如何构建服务器端web组件,为何要构建?
  • 78. Subsets
  • Angular 2 DI - IoC DI - 1
  • Computed property XXX was assigned to but it has no setter
  • extjs4学习之配置
  • gitlab-ci配置详解(一)
  • HTTP--网络协议分层,http历史(二)
  • JSDuck 与 AngularJS 融合技巧
  • k8s 面向应用开发者的基础命令
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Python进阶细节
  • select2 取值 遍历 设置默认值
  • VuePress 静态网站生成
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 安卓应用性能调试和优化经验分享
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 测试如何在敏捷团队中工作?
  • 电商搜索引擎的架构设计和性能优化
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 计算机在识别图像时“看到”了什么?
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 坑!为什么View.startAnimation不起作用?
  • 设计模式走一遍---观察者模式
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 王永庆:技术创新改变教育未来
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • Mac 上flink的安装与启动
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #{}和${}的区别是什么 -- java面试
  • #【QT 5 调试软件后,发布相关:软件生成exe文件 + 文件打包】
  • #Z2294. 打印树的直径
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (Java数据结构)ArrayList
  • (ros//EnvironmentVariables)ros环境变量
  • (二)linux使用docker容器运行mysql
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)springboot课程在线考试系统 毕业设计 655127