深刻理解代替单纯记忆html
最开始(iOS 7)是NSLayoutConstraint
表示约束,直接加到视图中生效ios
后来(iOS 9),因为直接添加约束的API太冗长,因而出现了NSLayoutAnchor
的一套API安全
view1.topAnchor.constraint(equalTo: view2.centerYAnchor).isActive = true
复制代码
同时出现了Top/Bottom Layout Guide
,为的是解决在与root view of viewcontroller
布局时,躲开顶部导航栏、状态栏的问题(由于若是没有这两个guide,代码中可能会出现一些magic number)bash
topLayoutGuide是UILayoutSupport
protocol类型app
length
用于framebu'justtopAnchor
、bottomAnchor
、length
用于autolayout布局Auto Layout Guideiview
iOS 11及以后便
deprecated
了,取而代之的是更好用的safeArea
ide
一种在view、subviews之间起做用的相对布局机制;能够看做是不建议subview布局的区域布局
layoutMargins
和layoutMarginsGuide
两个属性(对应frame和autolayout两种布局方式)layoutMargins
表示的范围layoutMarginsGuide
或在IB中选择相对于margin布局时,当父视图的layoutMargins
发生改变时,子视图位置会相对改变preservesSuperviewLayoutMargins
属性为true时(默认false),也会联动变化。变化是,设置该属性的view,在subview布局时,会考虑view.superView所设置的layoutMargins,好比view.superView的layoutMargins设置的很大,以致于subview可能会布局到view.superview不但愿的地方,那开启此属性就会自动调整subview位置directionalLayoutMargins
,是layoutMargin
的升级版,layoutMaring
在水平方向只有left和right,但directionalLayoutMargins
使用了leading和trailing,支持虽不一样阅读方向的语言的环境下动态配置横向marginPositioning Content Within Layout Marginsui
safeAreaLayoutGuide
用于autolayoutsafeAreaInsets
用于frame布局additionalSafeAreaInsets
来扩展safeareaPositioning Content Relative to the Safe Areaspa
截止到目前(写文章时最新的iOS系统是13版本)
safearea的产生并非用来取代layoutMargin的,从layoutMargin并无被废弃可以看出,这两套机制均可以正常的work
layoutMargin能够被修改,safeArea不能够
二者的惟一联系是,insetsLayoutMarginsFromSafeArea
当为true时,且layoutMargin在safearea范围之外时,layoutMargin会自动调整为safeArea的大小
Layout Guide, Margins, Insets and Safe Area demystified on iOS 10 & 11