adjustedContentInset | safeAreaInset | contentInset | addtionalSafeAreaInset

iOS 11中Controller的automaticallyAdjustsScrollViewInsets属性被废弃了.
替代的是上文说的UIScrollerView中的contentInsetAdjustmentBehavior.
这个属性是枚举类型:

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
        UIScrollViewContentInsetAdjustmentAutomatic, 
        UIScrollViewContentInsetAdjustmentScrollableAxes, 
        UIScrollViewContentInsetAdjustmentNever, 
        UIScrollViewContentInsetAdjustmentAlways,
    } API_AVAILABLE(ios(11.0),tvos(11.0));

如何理解这几个枚举类型的区别和意思需要先弄明白下面几个属性.

safeAreaInset

先说一下什么是安全区域,安全区域帮助我们将view放置在整个屏幕的可视的部分。即使把navigationbar设置为透明的,系统也认为安全区域是从navigationbar的bottom开始,保证不被系统的状态栏、或导航栏覆盖。

safeAreaInsets属性反映了一个view距离该view的安全区域的边距。换一句更容易理解的是:去掉StatusBar、NavigationBar、TabBar等视图,距离屏幕上下左右的距离. safeAreaInsets并不是一个需要设置的属性,系统会根据各种Bar的情况自己计算(包括在iphoneX上下的距离计算).

contentInset

contentInset在上一篇文章中介绍过了,表示一个ScrollerView的内容周围增加的边距,这个系统默认是0,需要手动的去调整设置.

adjustedContentInset

自适应内容边距, contentInsetAdjustmentBehavior的各种值设置的就是这个属性.

1.UIScrollViewContentInsetAdjustmentAutomatic:
如果scrollview在一个automaticallyAdjustsScrollViewContentInset = YES的controller上,并且这个Controller包含在一个navigationController中,这种情况下会设置在top & bottom上 adjustedContentInset = safeAreaInset + contentInset(不管是否滚动)。其他情况下与UIScrollViewContentInsetAdjustmentScrollableAxes相同.

2.UIScrollViewContentInsetAdjustmentScrollableAxes:
在可滚动方向上adjustedContentInset = safeAreaInset + contentInset,在不可滚动方向上adjustedContentInset = contentInset;依赖于scrollEnabled和alwaysBounceHorizontal / vertical = YES,scrollEnabled默认为yes,所以大多数情况下,计算方式还是adjustedContentInset = safeAreaInset + contentInset.

3.UIScrollViewContentInsetAdjustmentNever:
adjustedContentInset = contentInset

4.UIScrollViewContentInsetAdjustmentAlways:
adjustedContentInset = safeAreaInset + contentInset

addtionalSafeAreaInset

通过此设置可以修改safeAreaInset,进而影响内容视图的位置.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容