热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS开发之Autolayout

1、概述在以前的iOS程序中,是如何设置布局UI界面的?(1)经常编写大量的坐标计算代码(2)为了保证在3.5inch和4.0inch屏幕上都能有完美的UI界面效果,有时还需要分别

1、概述

在以前的iOS程序中,是如何设置布局UI界面的?

(1)经常编写大量的坐标计算代码

(2)为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还需要分别为2种屏幕编写不同的坐标计算代码(即传说中的“屏幕适配”)

什么是Autolayout

(1)Autolayout是一种“自动布局”技术,专门用来布局UI界面的

(2)Autolayout自iOS 6开始引入,由于Xcode 4的不给力,当时并没有得到很大推广

(3)自iOS 7(Xcode 5)开始,Autolayout的开发效率得到很大的提升

(4)苹果官方也推荐开发者尽量使用Autolayout来布局UI界面

(5)Autolayout能很轻松地解决屏幕适配的问题

Autoresizing

(1)在Autolayout之前,有Autoresizing可以作屏幕适配,但局限性较大,有些任务根本无法完成

(2)相比之下,Autolayout的功能比Autoresizing强大很多

Autolayout2个核心概念:

(1)参照

(2)约束

2Autolayout的警告和错误

警告:

控件的frame不匹配所添加的约束, 比如:

约束控件的宽度为100, 而控件现在的宽度是110

错误:

缺乏必要的约束, 比如:

只约束了宽度和高度, 没有约束具体的位置

两个约束冲突, 比如:

1个约束控件的宽度为100, 1个约束控件的宽度为110

3、代码实现Autolayout

代码实现Autolayout的步骤:

第一步:利用NSLayoutConstraint类创建具体的约束对象

第二步:添加约束对象到相应的view上

- (void)addConstraint:(NSLayoutConstraint *)constraint;

- (void)addConstraints:(NSArray *)constraints;

代码实现Autolayout的注意点:

(1)要先禁止autoresizing功能,设置view的下面属性为NO

view.translatesAutoresizingMaskIntoCOnstraints= NO;

(2)添加约束之前,一定要保证相关控件都已经在各自的父控件上

(3)不用再给view设置frame

4NSLayoutConstraint

一个NSLayoutConstraint对象就代表一个约束。

创建约束对象的常用方法:

+(id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

view1 :要约束的控件

attr1 :约束的类型(做怎样的约束)

relation :与参照控件之间的关系

view2 :参照的控件

attr2 :约束的类型(做怎样的约束)

multiplier :乘数

c :常量

自动布局有个核心公式:

obj1.property1 =(obj2.property2 * multiplier)+ constant value

例如:实现下面效果:

技术分享

- (void)viewDidLoad

{

    [super viewDidLoad];

    // 1.添加控件

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

    blueView.backgroundColor = [UIColor blueColor];

    blueView.translatesAutoresizingMaskIntoCOnstraints= NO;

    [self.view addSubview:blueView];

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

    redView.backgroundColor = [UIColor redColor];

    redView.translatesAutoresizingMaskIntoCOnstraints= NO;

    [self.view addSubview:redView];

    // 2.约束蓝色

    // 2.1.高度

NSLayoutConstraint *blueHeight =

[NSLayoutConstraint constraintWithItem:blueView attribute:

NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:

nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0

constant:40];

    [blueView addConstraint:blueHeight];

    // 2.2.左边间距

    CGFloat margin = 20;

NSLayoutConstraint *blueLeft =

[NSLayoutConstraint constraintWithItem:blueView attribute:

NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:

self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:

margin];

    [self.view addConstraint:blueLeft];

    // 2.3.顶部间距

NSLayoutConstraint *blueTop =

[NSLayoutConstraint constraintWithItem:blueView attribute:

NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:

self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:

margin];

    [self.view addConstraint:blueTop];

    // 2.4.右边间距

NSLayoutConstraint *blueRight =

 [NSLayoutConstraint constraintWithItem:blueView attribute:

NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:

self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:

-margin];

    [self.view addConstraint:blueRight];

    // 3.约束红色

    // 3.1.让红色右边 == 蓝色右边

NSLayoutConstraint *redRight =

[NSLayoutConstraint constraintWithItem:redView attribute:

NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:

blueView attribute:NSLayoutAttributeRight multiplier:1.0

constant:0.0];

    [self.view addConstraint:redRight];

    // 3.2.让红色高度 == 蓝色高度

NSLayoutConstraint *redHeight =

[NSLayoutConstraint constraintWithItem:redView attribute:

NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:

blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:

0.0];

    [self.view addConstraint:redHeight];

    // 3.3.让红色顶部 == 蓝色底部 + 间距

NSLayoutConstraint *redTop =

[NSLayoutConstraint constraintWithItem:redView attribute:

NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:

blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:

margin];

    [self.view addConstraint:redTop];

    // 3.4.让红色宽度 == 蓝色宽度 * 0.5

NSLayoutConstraint *redWidth =

[NSLayoutConstraint constraintWithItem:redView attribute:

NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:

blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:

0.0];

    [self.view addConstraint:redWidth];

}

5、添加约束的规则

在创建约束之后,需要将其添加到作用的view上。

在添加时要注意目标view需要遵循以下规则:

(1)对于两个同层级view之间的约束关系,添加到它们的父view上

技术分享

(2)对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上

技术分享

(3)对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上

技术分享

6VFL语言

什么是VFL语言?

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

技术分享

7VFL示例

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]-[FindField(>=20)]-|

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

8VFL的使用

使用VFL来创建约束数组:

+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;

format :VFL语句

opts :约束类型

metrics :VFL语句中用到的具体数值

views :VFL语句中用到的控件

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

NSDictionaryOfVariableBindings(...)

例如:

NSDictionary *views =

NSDictionaryOfVariableBindings(blueView, redView);

NSArray *conts2 =

[NSLayoutConstraint constraintsWithVisualFormat:

@"V:[blueView(==blueHeight)]-margin-|" options:0 metrics:

@{@"blueHeight" : @40, @"margin" : @20} views:views];

示例代码:

- (void)viewDidLoad

{

    [super viewDidLoad];

    // 1.添加控件

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

    blueView.backgroundColor = [UIColor blueColor];

    blueView.translatesAutoresizingMaskIntoCOnstraints= NO;

    [self.view addSubview:blueView];

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

    redView.backgroundColor = [UIColor redColor];

    redView.translatesAutoresizingMaskIntoCOnstraints= NO;

    [self.view addSubview:redView];

    // 2.VFL生成约束

NSArray *cOnts=

 [NSLayoutConstraint constraintsWithVisualFormat:

@"H:|-20-[blueView]-20-|" options:0 metrics:nil views:

@{@"blueView" : blueView}];

    [self.view addConstraints:conts];

NSArray *conts2 =

 [NSLayoutConstraint constraintsWithVisualFormat:

@"V:|-20-[blueView(40)]-20-[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}];

    [self.view addConstraints:conts2];

NSLayoutConstraint *redWidth =

 [NSLayoutConstraint constraintWithItem:redView attribute:

NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:

blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:

0.0];

    [self.view addConstraint:redWidth];

}}

运行效果图:

技术分享

9有了AutolayoutUILabel

在没有Autolayout之前,UILabel的文字内容总是居中显示,导致顶部和底部会有一大片空缺区域:

技术分享

有Autolayout之后,UILabel的bounds默认会自动包住所有的文字内容,顶部和底部不再会有空缺区域:

技术分享

10基于Autolayout的动画

在修改了约束之后,只要执行下面代码,就能做动画效果:

[UIView animateWithDuration:1.0 animations:^{

    [添加了约束的view layoutIfNeeded];

}];

例如:

self.leftMargin.cOnstant= 100;

self.width.cOnstant= 200;

[UIView animateWithDuration:2.0 animations:^{

     //父控件和redView都添加了约束

     [self.view layoutIfNeeded];

     [self.redView layoutIfNeeded];

}];

iOS开发之Autolayout


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 今天就跟大家聊聊有关怎么在Android应用中实现一个换肤功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文介绍了腾讯最近开源的BERT推理模型TurboTransformers,该模型在推理速度上比PyTorch快1~4倍。TurboTransformers采用了分层设计的思想,通过简化问题和加速开发,实现了快速推理能力。同时,文章还探讨了PyTorch在中间层延迟和深度神经网络中存在的问题,并提出了合并计算的解决方案。 ... [详细]
  • 本文介绍了响应式页面的概念和实现方式,包括针对不同终端制作特定页面和制作一个页面适应不同终端的显示。分析了两种实现方式的优缺点,提出了选择方案的建议。同时,对于响应式页面的需求和背景进行了讨论,解释了为什么需要响应式页面。 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • android 触屏处理流程,android触摸事件处理流程 ? FOOKWOOD「建议收藏」
    android触屏处理流程,android触摸事件处理流程?FOOKWOOD「建议收藏」最近在工作中,经常需要处理触摸事件,但是有时候会出现一些奇怪的bug,比如有时候会检测不到A ... [详细]
  • SmartRefreshLayout自定义头部刷新和底部加载
    1.添加依赖implementation‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.3’implementation‘com.s ... [详细]
  • Java和JavaScript是什么关系?java跟javaScript都是编程语言,只是java跟javaScript没有什么太大关系,一个是脚本语言(前端语言),一个是面向对象 ... [详细]
author-avatar
PHP》小魏
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有