[iOS]Auto Layout 代码约束

2024-08-20 23:32
文章标签 代码 auto ios 约束 layout

本文主要是介绍[iOS]Auto Layout 代码约束,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[iOS]Auto Layout 代码约束

Demo:http://download.csdn.net/detail/u012881779/9517858

自动布局的优势就不用提了,不管是竖屏、横屏或是在不同分别率的机器上运行,都能完美适应。当然,在实际使用中有时可能是个笑话。

布局的核心是添加约束,用代码添加约束这里有两种方式,分使用VFL(Visual format language)视觉形式语言和不使用VFL。

可以作个区别比较:

使用VFL

- (void)viewDidLoad {[super viewDidLoad];[self.view setBackgroundColor:[UIColor blackColor]];/*首先,心里要有约束对象的样式比如这样:__ _________ __*/UIView *oneView = [[UIView alloc] init];UIView *twoView = [[UIView alloc] init];UIView *threeView = [[UIView alloc] init];UIView *fourView = [[UIView alloc] init];UIView *fiveView = [[UIView alloc] init];[oneView setBackgroundColor:[UIColor redColor]];[twoView setBackgroundColor:[UIColor yellowColor]];[threeView setBackgroundColor:[UIColor blueColor]];[fourView setBackgroundColor:[UIColor orangeColor]];[fiveView setBackgroundColor:[UIColor greenColor]];//添加约束之前,必须将view添加到superview[self.view addSubview:oneView];[self.view addSubview:twoView];[self.view addSubview:threeView];[self.view addSubview:fourView];[self.view addSubview:fiveView];//对于要使用Auto Layout的控件需要关闭Autoresizing[oneView setTranslatesAutoresizingMaskIntoConstraints:NO];[twoView setTranslatesAutoresizingMaskIntoConstraints:NO];[threeView setTranslatesAutoresizingMaskIntoConstraints:NO];[fourView setTranslatesAutoresizingMaskIntoConstraints:NO];[fiveView setTranslatesAutoresizingMaskIntoConstraints:NO];/*使用VFL方式约束*/NSMutableDictionary *conDict = [[NSMutableDictionary alloc] init];[conDict setObject:oneView forKey:@"oneObj"];[conDict setObject:twoView forKey:@"twoObj"];[conDict setObject:threeView forKey:@"threeObj"];[conDict setObject:fourView forKey:@"fourObj"];[conDict setObject:fiveView forKey:@"fiveObj"];NSMutableDictionary *metricsDict = [[NSMutableDictionary alloc] init];[metricsDict setObject:[NSNumber numberWithFloat:11.1] forKey:@"dev"];/*"H:" 水平约束"V:" 垂直约束"|"  相当于superview的边界"-"  是连接符,表示两者间的间距,可以省略表示无间距"(==x)" 在括号里约束控件本身的宽/高,x可以是实数(==10.0)约束格式:@"H:||";@"H:|--|";@"H:|-[]-|";@"H:|-[oneObj]-|";描述:水平方向上有一个控件,它距离父视图左边界的距离为默认距离,距离父视图右边界的距离也为默认距离@"H:|-10-[oneObj]-20-|";描述:水平方向上有一个控件,它距离父视图左边界的距离是10,距离父视图右边界的距离是20@"V:|-[]-[]-[]-|";@"V:|-[oneObj]-[twoObj]-[threeObj]-|";@"V:|-10-[oneObj(==threeObj)]-[twoObj(==threeObj)]-[threeObj]-20-|";描述:垂直方向上有三个控件(one,two,three)one距离父视图上边界的距离是10,并且高度与three的高度相等two距离one的下边界距离为默认距离,并且高度与three的高度相等three距离two的下边界距离为默认距离,同时距离父视图的下边界距离是20*///水平关系(H:)NSString *oneHVFL = @"H:|-dev-[oneObj(==twoObj)]-[twoObj]-dev-|";NSString *twoHVFL = @"H:|-dev-[threeObj]-dev-|";NSString *threeHVFL = @"H:|-dev-[fourObj(==fiveObj)]-[fiveObj]-dev-|";//垂直关系(V:)NSString *oneVVFL = @"V:|-dev-[oneObj(==fourObj)]-[threeObj(==fourObj)]-[fourObj]-dev-|";NSString *twoVVFL = @"V:|-dev-[twoObj(==fiveObj)]-[threeObj(==fiveObj)]-[fiveObj]-dev-|";/*添加约束[self.view addConstraint:<#(nonnull NSLayoutConstraint *)#>];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:<#(nonnull NSString *)#> options:<#(NSLayoutFormatOptions)#> metrics:(nullable NSDictionary<NSString *,id> *) views:<#(nonnull NSDictionary<NSString *,id> *)#>]];注意:对于某个控件来说,横向约束和纵向约束都必须要有,不然视图无法呈现*///横向约束[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:oneHVFL options:0 metrics:metricsDict views:conDict]];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:twoHVFL options:0 metrics:metricsDict views:conDict]];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:threeHVFL options:0 metrics:metricsDict views:conDict]];//纵向约束[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:oneVVFL options:0 metrics:metricsDict views:conDict]];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:twoVVFL options:0 metrics:metricsDict views:conDict]];//更新约束[self.view setNeedsUpdateConstraints];[self.view updateConstraintsIfNeeded];
}

不用VFL

- (void)viewDidLoad {[super viewDidLoad];UIView *oneView = [[UIView alloc] init];UIView *twoView = [[UIView alloc] init];UIView *threeView = [[UIView alloc] init];UIView *fourView = [[UIView alloc] init];UIView *fiveView = [[UIView alloc] init];[oneView setBackgroundColor:[UIColor redColor]];[twoView setBackgroundColor:[UIColor yellowColor]];[threeView setBackgroundColor:[UIColor blueColor]];[fourView setBackgroundColor:[UIColor orangeColor]];[fiveView setBackgroundColor:[UIColor greenColor]];//添加约束之前,必须将view添加到superview[self.view addSubview:oneView];[self.view addSubview:twoView];[self.view addSubview:threeView];[self.view addSubview:fourView];[self.view addSubview:fiveView];//对于要使用Auto Layout的控件需要关闭Autoresizing[oneView setTranslatesAutoresizingMaskIntoConstraints:NO];[twoView setTranslatesAutoresizingMaskIntoConstraints:NO];[threeView setTranslatesAutoresizingMaskIntoConstraints:NO];[fourView setTranslatesAutoresizingMaskIntoConstraints:NO];[fiveView setTranslatesAutoresizingMaskIntoConstraints:NO];/*对各控件分别添加约束公式的约束形式为:  "view1.attr1 = view2.attr2 * multiplier + constant"[self.view addConstraint:[NSLayoutConstraint constraintWithItem:<#(nonnull id)#>attribute:<#(NSLayoutAttribute)#>relatedBy:<#(NSLayoutRelation)#>toItem:<#(nullable id)#>attribute:<#(NSLayoutAttribute)#>multiplier:<#(CGFloat)#>constant:<#(CGFloat)#>]];typedef NS_ENUM(NSInteger, NSLayoutAttribute) {NSLayoutAttributeLeft = 1,  //左侧NSLayoutAttributeRight,     //右侧NSLayoutAttributeTop,       //上侧NSLayoutAttributeBottom,    //下侧NSLayoutAttributeLeading,   //领先 左侧NSLayoutAttributeTrailing,  //落后 右侧NSLayoutAttributeWidth,NSLayoutAttributeHeight,NSLayoutAttributeCenterX,NSLayoutAttributeCenterY,NSLayoutAttributeBaseline,NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),NSLayoutAttributeNotAnAttribute = 0};typedef NS_ENUM(NSInteger, NSLayoutRelation) {NSLayoutRelationLessThanOrEqual = -1,NSLayoutRelationEqual = 0,NSLayoutRelationGreaterThanOrEqual = 1,};*///one[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeLeftrelatedBy:NSLayoutRelationEqualtoItem:oneViewattribute:NSLayoutAttributeLeftmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:oneViewattribute:NSLayoutAttributeTopmultiplier:1constant:-10]];//two[self.view addConstraint:[NSLayoutConstraint constraintWithItem:oneViewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:twoViewattribute:NSLayoutAttributeLeftmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:twoViewattribute:NSLayoutAttributeTopmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:twoViewattribute:NSLayoutAttributeRightmultiplier:1constant:10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:oneViewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:twoViewattribute:NSLayoutAttributeBottommultiplier:1constant:0]];//three[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeLeftrelatedBy:NSLayoutRelationEqualtoItem:threeViewattribute:NSLayoutAttributeLeftmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:threeViewattribute:NSLayoutAttributeRightmultiplier:1constant:10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:oneViewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:threeViewattribute:NSLayoutAttributeTopmultiplier:1constant:-10]];//four[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeLeftrelatedBy:NSLayoutRelationEqualtoItem:fourViewattribute:NSLayoutAttributeLeftmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:threeViewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:fourViewattribute:NSLayoutAttributeTopmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:fourViewattribute:NSLayoutAttributeBottommultiplier:1constant:10]];//five[self.view addConstraint:[NSLayoutConstraint constraintWithItem:fourViewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:fiveViewattribute:NSLayoutAttributeLeftmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:threeViewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:fiveViewattribute:NSLayoutAttributeTopmultiplier:1constant:-10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeRightrelatedBy:NSLayoutRelationEqualtoItem:fiveViewattribute:NSLayoutAttributeRightmultiplier:1constant:10]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:fiveViewattribute:NSLayoutAttributeBottommultiplier:1constant:10]];//width&height[self.view addConstraint:[NSLayoutConstraint constraintWithItem:oneViewattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:twoViewattribute:NSLayoutAttributeWidthmultiplier:1constant:0]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:oneViewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:threeViewattribute:NSLayoutAttributeHeightmultiplier:1constant:0]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:fourViewattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:fiveViewattribute:NSLayoutAttributeWidthmultiplier:1constant:0]];[self.view addConstraint:[NSLayoutConstraint constraintWithItem:threeViewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:fourViewattribute:NSLayoutAttributeHeightmultiplier:1constant:0]];//更新约束[self.view setNeedsUpdateConstraints];[self.view updateConstraintsIfNeeded];}

需要注意的地方:

约束是添加在父视图上;

由于约束使用的相对位置,所以父视图与被约束这些视图之间必须有一个视图的size是确定的;

对于某个被约束的视图,必须要能定到位(left、top、right、bottom、width、height),不然视图无法呈现(参照文末“未完整约束时”效果图);


使用VFL效果图:

竖屏

横屏


未完整约束时:

//[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:threeHVFL options:0 metrics:metricsDict views:conDict]];

//[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:twoVVFL options:0 metrics:metricsDict views:conDict]];



不使用VFL效果图:


这篇关于[iOS]Auto Layout 代码约束的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091485

相关文章

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计