默认设置是 UIRectEdgeAll,即viewController的View会延伸到最顶端,分别设置为UIRectEdgeNone和UIRectEdgeAll用reveal来看看效果.
1、 UIRectEdgeNone
![](http://upload-images.jianshu.io/upload_images/1448065-cfee064f75c87cb5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/510/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-0c991c4d87d52852.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/821/format/webp)
上图中画圈圈的Y是64,说明view刚好从导航栏下面开始的。
2、UIRectEdgeAll
![](http://upload-images.jianshu.io/upload_images/1448065-c1a374e1e0ef0255.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/578/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-ae6bee3da07643cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/816/format/webp)
这个已经很明显了
3、translucent设置为YES
- translucent 字面意思就是半透明,默认值是 YES
![](http://upload-images.jianshu.io/upload_images/1448065-a16243e8608845af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/649/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-da261c8299fc09be.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/414/format/webp)
上图可以看出,在设置translucent=YES的时候(默认就是YES,其实不写也一样),导航栏明显有点偏蓝
4、translucent设置为NO:
![](http://upload-images.jianshu.io/upload_images/1448065-4553943f2847d8ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/616/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-77bc87c5a7f15fcb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/414/format/webp)
对比也很明显。当transulent=NO的时候,不管edgesForExtendedLayout设置成UIRectEdgeAll还是UIRectEdgeNone,view都是从导航栏底部开始
,下面会有解释。
5、UIRectEdgeNone的情况
上面3和4都是在UIRectEdgeAll的情况下设置的,下面再看看UIRectEdgeNone的情况下呢,
transulent=YES的时候:
![](http://upload-images.jianshu.io/upload_images/1448065-5536ac35bcc4c449.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/644/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-e4904400a780ffff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/414/format/webp)
这时候导航栏竟然变灰色了,既不是半透明的蓝色,也不是白色,为什么是灰色?在reveal中看看视图结构就知道了:
![](http://upload-images.jianshu.io/upload_images/1448065-a2376dab0b3f8ed2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
原来是UIWindow的是黑色的,半透明一下就是灰色了,红色圈起来的部分写明了Background=Black Color;注意这和reveal工具的背景色还是有区别的。
transulent=NO的时候,这时候就比较特殊了,当transulent=NO的时候,不管edgesForExtendedLayout设置成UIRectEdgeAll还是UIRectEdgeNone,view都是从导航栏底部开始
![](http://upload-images.jianshu.io/upload_images/1448065-f159416dcd7646bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/586/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-89cbe1db896ff72d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/794/format/webp)
注意看红圈的y=64
![](http://upload-images.jianshu.io/upload_images/1448065-f4294e13a919d608.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/598/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-8cdbc855281e1980.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/794/format/webp)
同样红圈的y=64,
那么为题来了,怎么让translucent=NO的时候,view也能从(0,0)开始布局呢?
苹果也考虑到了这种需求,提供了 extendedLayoutIncludesOpaqueBars 这个属性。
extendedLayoutIncludesOpaqueBars 默认值是NO,下面把它改为YES
![](http://upload-images.jianshu.io/upload_images/1448065-7084e34a237a8141.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/634/format/webp)
使用了extendedLayoutIncludesOpaqueBars再来看看效果:
![](http://upload-images.jianshu.io/upload_images/1448065-3631262b19ad0200.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/787/format/webp)
此时view成功从(0,0)开始布局。
6、automaticallyAdjustsScrollViewInsets
还有一个属性:
- automaticallyAdjustsScrollViewInsets:
默认值YES,表示在全屏模式下会自动修改第一个添加到
rootView 的 scrollview 的 contentInset 为(64,0,0,0)
先设置automaticallyAdjustsScrollViewInsets为NO
automaticallyAdjustsScrollViewInsets在iOS11中已废弃,需要使用ScrollView子类的contentInsetAdjustmentBehavior属性来代替(比如写成self.tableView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;)
![](http://upload-images.jianshu.io/upload_images/1448065-c81af4a7927c5da8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/757/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-167ae9138b3cdea0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/414/format/webp)
此时tableView被导航栏遮盖。
再把automaticallyAdjustsScrollViewInsets设置为YES
![](http://upload-images.jianshu.io/upload_images/1448065-ae356550f0423c07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/507/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-20294094dea445e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/414/format/webp)
此时tableView从导航栏底部开始
需要注意的是automaticallyAdjustsScrollViewInsets=YES只对VC**第一个添加到 rootView 的 scrollview **有效。
如果我们在已经有了tableview的前提再加一个tableView,那么第二个tableView还是会被导航栏遮盖:
![](http://upload-images.jianshu.io/upload_images/1448065-77be8327a407b5b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/736/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-24e4dbfe09cdf13b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)
如上图所示,第二个tableView还是会被导航栏遮盖(红色箭头所指的0代表tableView的第一个cell)
安全区(safeArea)
在iOS11 中引入了安全区的概念,说白了就是:你放在这个区域里面的视图是不会被NavigationBar和TabBar和StatusBar遮住的.(前提是你不去手动修改安全区范围的情况下),
安全区本身不是一个View,不会显示在我们的视图层级上,只是给你参考用.
![](http://upload-images.jianshu.io/upload_images/1448065-e7a8aeb661442663.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/746/format/webp)
![](http://upload-images.jianshu.io/upload_images/1448065-be154eb0a987c6af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/950/format/webp)
safeAreaInset属性
iOS11之前,如果有tableview向下偏移64的情况的话.64这个值是在contentInset里面获取的,iOS11之后改成了从safeAreaInset获取.但是contentInset这个属性 !!!!依旧是有用的,并没有废弃. !!!!
contentInsetAdjustmentBehavior属性
上面的说过的automaticallyAdjustsScrollViewInsets属性在iOS 11中已经被废弃了(设置这个没用了),改用UIScrollview子类的contentInsetAdjustmentBehavior属性来代替
例如
self.tableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;//使用这行代码来代替
contentInsetAdjustmentBehavior属性有4个枚举值:{
UIScrollViewContentInsetAdjustmentAutomatic:在有导航栏的VC中,这个属性会设置上部和底部的adjustedContentInset值,
方式为adjustedContentInset = safeAreaInset + contentInset。其他情况下与UIScrollViewContentInsetAdjustmentScrollableAxes相同
UIScrollViewContentInsetAdjustmentScrollableAxes: 在可滚动方向上adjustedContentInset = safeAreaInset + contentInset,在不可滚动方向上adjustedContentInset = contentInset
UIScrollViewContentInsetAdjustmentNever: adjustedContentInset = contentInset
UIScrollViewContentInsetAdjustmentAlways: adjustedContentInset = safeAreaInset + contentInset
}