contentInsetAdjustmentBehavior各个值之间的区别
iOS11也出了不少时候了网上说适配的文章一大堆。
关于contentInsetAdjustmentBehavior这个参数之间的区别,好像没什么人能说明。
往下看的前提是你已经知道什么是安全区域,没看明白这个请出门左转WWDC2017编号204(16分20秒开始)。
以下内容是基于腾讯Bugly的iOS 11 安全区域适配总结内容扩展而来。(原文网址:https://mp.weixin.qq.com/s/W1_0VrchCO50owhJNmJnuQ)
这里写了个Demo目的是解释清楚contentInsetAdjustmentBehavior这个参数的值都是干啥的。
这个Demo的基本原则就是列出所有ScrollView的ContentSize和SaveArea的关系。
github地址:
https://github.com/biosli/ScrollViewContentInsetAdjustmentBehaviorDemo
基础代码
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: self.view.bounds];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; UIImage *testImg = [UIImage imageNamed: @"aaaa"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: testImg]; [self.view addSubview: scrollView];
简单叙述页面关系:
就是一个ViewController里面放一个ScrollView,ScrollView里面包含了一个UIImageView。aaaa是张大图。
一、UIScrollViewContentInsetAdjustmentScrollableAxes
例1:
//以下全部例子,请在iPhoneX模拟器上查看。
//UIScrollViewContentInsetAdjustmentScrollableAxes例1
//如果scrollView的ContentSize很小,则不考虑安全区域
CGRect frame = imageView.frame;
frame.size.width = ;
frame.size.height = ;
imageView.frame = frame;
[scrollView addSubview: imageView]; scrollView.contentSize = imageView.frame.size; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentScrollableAxes;
例1图:

这两个地方忽略了安全区域。
例2:
//UIScrollViewContentInsetAdjustmentScrollableAxes例子2,横屏查看
//如果scrollView的ContentSize大于超出显示范围,则计算安全区域
CGRect frame = imageView.frame;
frame.size.width = ;
frame.size.height = ;//图片拉长,超出屏幕范围
imageView.frame = frame;
[scrollView addSubview: imageView]; scrollView.contentSize = imageView.frame.size; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentScrollableAxes;
例2图:

被拉长了,纵向可以滚动,横向宽度不够不能滚动。
红色是横向方向,不可滚动,安全区域被忽略。
例3:
//UIScrollViewContentInsetAdjustmentScrollableAxes例子3,接上例,横屏查看
//如果强制横向滚动,则计算安全区域
CGRect frame = imageView.frame;
frame.size.width = ;
frame.size.height = ;//图片拉长,超出屏幕范围
imageView.frame = frame;
[scrollView addSubview: imageView]; scrollView.contentSize = imageView.frame.size;
//强制横向滚动
scrollView.alwaysBounceHorizontal = YES; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentScrollableAxes;
例3图:

强制横向滚动,两个方向的安全区域都会被考虑到。
二、UIScrollViewContentInsetAdjustmentAutomatic
//UIScrollViewContentInsetAdjustmentAutomatic例子,横屏查看
//对照UIScrollViewContentInsetAdjustmentScrollableAxes例1
//就算不够高度,也会空出上下两部分的安全区域。
CGRect frame = imageView.frame;
frame.size.width = ;
frame.size.height = ;
imageView.frame = frame;
[scrollView addSubview: imageView]; scrollView.contentSize = imageView.frame.size; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
例图:

图片很小,不够撑满屏幕,但是用这个参数,会空出上下方向的安全区域。
其他的行为与UIScrollViewContentInsetAdjustmentScrollableAxes一致。
三、UIScrollViewContentInsetAdjustmentNever
//UIScrollViewContentInsetAdjustmentNever例子
//完全不考虑安全区域
CGRect frame = imageView.frame;
frame.size.width = ;
frame.size.height = ;
imageView.frame = frame; [scrollView addSubview: imageView]; scrollView.contentSize = imageView.frame.size; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
例图:

Never了就是都不考虑安全区域了。
四、UIScrollViewContentInsetAdjustmentAlways
//UIScrollViewContentInsetAdjustmentAlways例子
//不管内容,全部考虑安全区域
CGRect frame = imageView.frame;
frame.size.width = ;
frame.size.height = ;
imageView.frame = frame; [scrollView addSubview: imageView]; scrollView.contentSize = imageView.frame.size; scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
例图:

不管内容够不够大,全部考虑安全区域。
(对比UIScrollViewContentInsetAdjustmentScrollableAxes例1和UIScrollViewContentInsetAdjustmentAutomatic例子)
以下是适配建议:
对于完全自定义页面的安全区域是个碍事的东西,把ScrollView(及子类)的contentInsetAdjustmentBehavior设置成Never,自己控制每一个细节。
对于没有横屏需求的同学,系统默认的UIScrollViewContentInsetAdjustmentAutomatic是个好选择,就不用使用者自己修改了。
对于有横屏需求的同学,建议使用UIScrollViewContentInsetAdjustmentAlways,横屏的时候不会被“刘海”干扰。
PS:后面是一些有趣的阴谋论,关于为什么苹果放弃了之前的automaticallyAdjustsScrollViewInsets而强制使用新的SafeArea。
iOS 11是6月WWDC发布的,那时候这个接口(automaticallyAdjustsScrollViewInsets)就被禁掉了。9月份iPhoneX发布。
当时,大家只知道苹果换了一套布局体系。
从程序员角度看,6月份大家开始动手适配iOS11,开发人员不明所以,唾弃新体系的种种不便。9月份发布新的屏幕尺寸,新的体系发挥了价值,乖乖听话的开发人员已经适配完成了。
从苹果角度看,6月份发布了新体系,但并没有透露新体系对新机型出来以后对适配的影响。9月份出新手机了,然后得意的看着开发者“叫你们丫不早适配”。
这是一个中间组件提供方,强制修改接口的一个好办法。开发人员应该老老实实听苹果粑粑的话趁热适配新的iOS系统,而且要详细看每一个与老系统的对比,优先进行适配。
contentInsetAdjustmentBehavior各个值之间的区别的更多相关文章
- 编写高质量代码改善C#程序的157个建议——建议28:理解延迟求值和主动求值之间的区别
建议28:理解延迟求值和主动求值之间的区别 要理解延迟求值(lazy evaluation)和主动求值(eager evaluation),先看个例子: List<, , , , , , , , ...
- C# 如何捕获键盘按钮和组合键以及KeyPress/KeyDown事件之间的区别 (附KeyChar/KeyCode值)
1. 首先将窗口属性KeyPreview设为true,如果属性对话框中找不到,就直接在代码里添加: 2. 添加KeyPress / KeyDown事件: 1.KeyPress 和KeyDown .Ke ...
- 你真的会玩SQL吗?EXISTS和IN之间的区别
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- [转]ExtJs基础--Html DOM、Ext Element及Component三者之间的区别
要学习及应用好Ext框架,必须需要理解Html DOM.Ext Element及Component三者之间的区别. 每一个HTML页面都有一个层次分明的DOM树模型,浏览器中的所有内容都有相应的DOM ...
- iOS中assign,copy,retain之间的区别以及weak和strong的区别
@property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...
- javascrip中parentNode和offsetParent之间的区别
首先是 parentNode 属性,这个属性好理解,就是在 DOM 层次结构定义的上下级关系,如果元素A包含元素B,那么元素B就可以通过 parentElement 属性来获取元素A. 要明白 off ...
- 面试问题5:const 与 define 宏定义之间的区别
问题描述:const 与 define 宏定义之间的区别 (1) 编译器处理方式不同 define宏是在预处理阶段展开: const常量是编译运行阶段使用: (2) 类型和安全检查不同 ...
- 关于背景图相对父容器垂直居中问题 —— vertical-align 和 line-height 之间的区别
html css <div class="register-wrapper"> <div class="register"> &l ...
- 答:SQLServer DBA 三十问之一: char、varchar、nvarchar之间的区别(包括用途和空间占用);xml类型查找某个节点的数据有哪些方法,哪个效率高;使用存储 过程和使用T-SQL查询数据有啥不一样;
http://www.cnblogs.com/fygh/archive/2011/10/18/2216166.html 1. char.varchar.nvarchar之间的区别(包括用途和空间占用) ...
随机推荐
- Windows上安装并启动visdom
windows上安装visdom$ conda install visdomCollecting package metadata: ...working... doneSolving environ ...
- python+jenkins 构建节点环境编译器配置问题
python 编译器默认添加环境变量路径
- 模拟T1数字number
那么第一题首先非常水的一道题…… 看一下题 数字(number) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK拥有n个数,这n个数分别是a1,a2,… ...
- js 产生随机数
这里整理了几个产生随机数的方法: 1.generateUUID() //获取一个唯一数 function generateUUID() { var d = new Date().getTime(); ...
- struts2_E_commerce_maven
这是作业的第二题:使用struts实现电子商务网站,这是基于之前的代码的,所以,主要就是修改成为struts的mvc模式. 1.开始,先把以前做的一个maven项目修改成为另一个项目(简称重命名) 重 ...
- Swift使用AlamoFire超时设置和事件处理
一直在写swift项目,正好碰到服务器部署,请求超时或者请求失败的问题,页面就卡着不动了.顺手解决一下吧 差了些资料,说要设置超时时间 方法一: static let sharedSessionMan ...
- 20155321 《网络攻防》 Exp7 网络欺诈防范
20155321 <网络攻防> Exp7 网络欺诈防范 实验内容 简单应用SET工具建立冒名网站 因为钓鱼网站是在本机的http服务下使用,因此需要将SET工具的访问端口改为http默认的 ...
- 分类-MNIST(手写数字识别)
这是学习<Hands-On Machine Learning with Scikit-Learn and TensorFlow>的笔记,如果此笔记对该书有侵权内容,请联系我,将其删除. 这 ...
- 总结:C# 委托的全面理解
在说事件之前得先了解委托. 委托,外表看来和C/C++中函数指针没什么区别,但是本质上你才发现他其实就是个类!也就是说理解委托得从 这个两个方面去理解(单从一个方面去理解感觉就怪怪的呵呵!) 理解委托 ...
- Caffe源码中common文件分析
Caffe源码(caffe version:09868ac , date: 2015.08.15)中的一些重要头文件如caffe.hpp.blob.hpp等或者外部调用Caffe库使用时,一般都会in ...