[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

相关文章

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤