UIKit: Apps for Every Size and Shape
safeArea
即可以正常显示内容的部分。


可以通过 additionalSafeAreaInsets 来调整 safeArea 的大小。

经过调整,范围如下:
self.additionalSafeAreaInsets = UIEdgeInsets.init(top: 20, left: 30, bottom: 0, right: 10)

通过 safeAreaLayoutGuide 创建约束
bgView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
bgView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
bgView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
bgView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
subView 的 safeAreaInsets
- 完全在内部的,
safeAreaInsets为 0 - 部分在 supview 内部,但在 safeArea 之外的,
safeAreaInsets为之间的大小 - 在 superview 之外的,为父 view
safeAreaInsets的大小,不会延伸出去

view safeArea: UIEdgeInsets(top: 64.0, left: 30.0, bottom: 83.0, right: 10.0)
bgview SafeArea: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
topview SafeArea: UIEdgeInsets(top: 64.0, left: 10.0, bottom: 0.0, right: 0.0)
rightView SafeArea: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 10.0)
如上所示,蓝色完全在 rootview 的 safeArea 之内,所以 safeAreaInsets 为0.
topview 在 rootview 的 bounds 之内,但是在 safeArea 之外,所以 safeAreaInsets 反映了在 rootview 的 bounds 之内, safeArea 之外的区域。
rightView 的右边已经超出了屏幕范围,所以 safeAreaInsets 的右边是 rootview 的 safeAreaInsets 的右边,不会继续超出。
Margin
即内间距

使用 layoutMarginsGuide 来创建约束
bgView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
bgView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),
bgView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
bgView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor)
insetsLayoutMarginsFromSafeArea 属性
When the value of this property is true, any margins that are outside the safe area are automatically modified to fall within the safe area boundary. The default value of this property is true. Changing the value to false allows your margins to remain at their original locations, even when they are outside the safe area.
即这个属性可以保证不落在 safeArea 区域之外

// 这里可以修改 layoutmargin
topView.layoutMargins = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
[topView, rightView].forEach { (viewa) in
let label = UILabel.init()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = UIColor.purple
viewa?.addSubview(label)
label.text = "tesx"
let labelConstranits = [label.topAnchor.constraint(equalTo: viewa!.layoutMarginsGuide.topAnchor),
label.leftAnchor.constraint(equalTo: viewa!.layoutMarginsGuide.leftAnchor)]
NSLayoutConstraint.activate(labelConstranits)
// 如果这个是false,那么topview中的label,可以出现在safeArea区域之外 topView.layoutMargins维持为0
// 如果这个是true,那么topview的lable一定在safeArea之内。topView.layoutMargins也会变化
viewa!.insetsLayoutMarginsFromSafeArea = false
}
如上图所示,lable 落在了 safeArea 之外,如果把 insetsLayoutMarginsFromSafeArea 设置为 true,结果如下:

值得注意的是,创建约束时,一定要用 layoutMarginsGuide,否则,即使insetsLayoutMarginsFromSafeArea 设置为 true,也会落在 safeArea 之外。
preservesSuperviewLayoutMargins
When the value of this property is true, the superview’s margins are also considered when laying out content. This margin affects layouts where the distance between the edge of a view and its superview is smaller than the corresponding margin. For example, you might have a content view whose frame precisely matches the bounds of its superview. When any of the superview’s margins is inside the area represented by the content view and its own margins, UIKit adjusts the content view’s layout to respect the superview’s margins. The amount of the adjustment is the smallest amount needed to ensure that content is also inside the superview’s margins.
文档写的有点稀里糊涂的,我们看一个例子。

- 蓝色的 view 上加了一层紫色的 view, 紫色的 view 上加了白色的 view
- 蓝色 view 的
layoutMargins为(top: 100, left: 50, bottom: 100, right: 40) - 紫色 view 的约束为顶部的底部差10,左右展开
- 紫色 view 的
preservesSuperviewLayoutMargins为 true - 白色 view 的约束为上下左右和紫色 view 的
layoutMarginsGuide对齐 - 紫色 view 的
layoutMargins为(top: 0, left: 0, bottom: 0, right: 0)
如果 preservesSuperviewLayoutMargins 为 false,那么白色 view 和紫色 view 大小完全一致。由于这个属性为 true,所以紫色 view 在布局时,考虑到了父 view 的 layoutMargins,因此白色 view 大小被缩减了。
viewRespectsSystemMinimumLayoutMargins
系统的 VC 有一个默认的最小内间距,只读不可写,称为 systemMinimumLayoutMargins。
即使我们设置了 rootview 的 directionalLayoutMargins,如果太小的话,系统会会退到系统默认的最小间距。

如上所示,VC 的 rootView 的内间距被设为了零,按照 layoutMarginsGuide 创建约束,左右仍然有内间距。是因为这个VC 的 systemMinimumLayoutMargins 为 (top: 0.0, leading: 16.0, bottom: 0.0, trailing: 16.0)。
如果把 viewRespectsSystemMinimumLayoutMargins 设置为 false,就可以扩展到边缘了。

参考
- UIKit: Apps for Every Size and Shape
- Preserves Superview Layout Margins
- imanoupetit/Margins-And-Safe-Area
- demo
UIKit: Apps for Every Size and Shape的更多相关文章
- shape的简单用法
shap节点-----------------------------------定义shape的值,必须是下面的之一:"rectangle" 矩阵,这也是默认的shape&quo ...
- android shape使用总结
今天使用到shape,这个里面有很多属性,在这里我记录一下各个属性的使用的情况以及所代表的意思 <?xml version="1.0" encoding="utf- ...
- Android Drawable - Shape Drawable使用详解(附图)
TIPS shape图形 –简单介绍 shape图形 –如何画? shape图形 –参数详细解析 shape图形 –如何用? shape图形 –实际开发应用场景 shape图形简单介绍 用xml实现一 ...
- 每日英语:Missing at Mobile World Congress: Innovation
The hottest showcase for new technology at this year's Mobile World Congress wasn't in the event's c ...
- iOS8 对开发者来说意味着什么?
今天凌晨,Apple WWDC2014 iOS8 正式推出! 也许,对于广大iOS用户来说,iOS8的创新并不是特别多. 但对于开发者来说,影响却将会是无比巨大的! 正如Apple官网上的广告:Hug ...
- cocos2d-x 2.2.0 图片选中聚焦 ,图片描边 CCClippingNode 实现
效果例如以下图 左边箭头是x方向翻转的.右边箭头有旋转和缩放action. 大概实现方法:用箭头作为遮罩层,底图是一个绘制的矩形,得到一个黄色箭头背景.在用schedule尾随要聚焦箭头动作.这个 ...
- Auto Layout Guide----(一)-----Understanding Auto Layout
Understanding Auto Layout 理解自动布局 Auto Layout dynamically calculates the size and position of all the ...
- UI与数据分离 与 UI的演进
解藕的好处:UI内部模块能够灵活的变化. MVC或者三层架构着重强调了数据.业务逻辑和UI的分离. (MVC中的C只是UI和业务逻辑模块间的一个中转组件,理论上应该是个轻模块.) 以前的关注的解藕技术 ...
- iOS8 对开发人员来说意味着什么?
今天凌晨.Apple WWDC2014 iOS8 正式推出. 或许,对于广大iOS用户来说,iOS8的创新并非特别多. 但对于开发人员来说,影响却将会是无比巨大的! 正如Apple官网上的广告:Hug ...
随机推荐
- li.active2有加强重要性的效果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- sql server 2008安装的时候选NT AUTHORITY\NEWORK SERVICE 还是选 NT AUTHORITY\SYSTEM ?
sql server 2008安装的时候选NT AUTHORITY\NEWORK SERVICE 还是选 NT AUTHORITY\SYSTEM ? sql server 2008安装的时候选NT A ...
- 2018.06.29 NOIP模拟 繁星(前缀和)
繁星 [问题描述] 要过六一了,大川正在绞尽脑汁想送给小伙伴什么礼物呢.突然想起以前拍过一张夜空中的繁星的照片,这张照片已经被处理成黑白的,也就是说,每个像素只可能是两个颜色之一,白或黑.像素(x,y ...
- 2018.09.05 任务安排(斜率优化dp)
描述 这道题目说的是,给出了n项必须按照顺序完成的任务,每项任务有它需要占用机器的时间和价值.现在我们有一台机器可以使用,它每次可以完成一批任务,完成这批任务所需的时间为一个启动机器的时间S加上所有任 ...
- 深入浅析JavaScript中with语句的理解
JavaScript 有个 with 关键字, with 语句的原本用意是为逐级的对象访问提供命名空间式的速写方式. 也就是在指定的代码区域, 直接通过节点名称调用对象. with语句的作用是暂时改变 ...
- VHDL的库
STD_LOGIC_ARITH 扩展了UNSIGNED.SIGNED.SMALL_INT(短整型)三个数据类型,并定义了相关的算术运算和转换函数. --======================== ...
- Spring bean是如何加载的
Spring bean是如何加载的 加载bean的主要逻辑 在AbstractBeanFactory中doGetBean对加载bean的不同情况进行拆分处理,并做了部分准备工作 具体如下 获取原始be ...
- 201709015工作日记--IntentService使用
一.IntentService与Service的区别 Service 是 Android 四大组件之一,正常来说,我们直接使用 Service 就可以了. 但是 Service 存在几个问题: 默认不 ...
- hdu1257 最少拦截系统(贪心) 2016-05-19 20:28 90人阅读 评论(0) 收藏
最少拦截系统 Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能 ...
- float 为什么不能用== ,或者大于等于,或者小于等于
本文尝试着将以下内容做一个浅显的解释,主要包括浮点数为什么是不精确的,浮点数为什么不能用==和!=直接比较,以及浮点数的比较方法等几个方面.如果那个地方说的不对还请各位看官不吝赐教!欢迎大家评论区讨论 ...