UINavigationController + UIScrollView组合,视图尺寸的设置探秘(三)
还是在苹果的 View Controller Catalog for iOS 文章中找到答案。文中提到了两点:
1、If the navigation bar or toolbar are visible but not translucent, it does not matter if the view controller wants its view to be displayed using a full-screen layout.
如果navigation bar或者toolbar不透明,view controller就无法让它的view全屏显示。换句话说,如果不希望view controller里面的view全屏显示,就应该把navigation bar设为不透明。
2、If the main view of the underlying view controller is a scroll view, the navigation bar automatically adjusts the content inset value to allow content to scroll out from under the navigation bar. It does not make this adjustment for other types of views.
如果view controller下的主视图是scroll view,navigation bar 会自动调整其content inset的值,以便被盖在navigation bar底下的内容能被滚动出来,对于其他类型的视图则不会做此调整。
结论很清楚:要想解决navigation controller下scroll bar诡异的滚动问题,只需要把navigation bar设为不透明。再做一把实验,在viewDidLoad中插入一行,将navigationController.navigationBar.translucent属性设为NO:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
self.scrollView.pagingEnabled = YES;
CGRect rect = self.scrollView.frame;
rect.size.width *= ;
self.scrollView.contentSize = rect.size;
CGRect rtViewA = self.scrollView.frame;
rtViewA.origin.y = ;
UIView *viewA = [[UIView alloc]initWithFrame:rtViewA];
viewA.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:viewA];
CGRect rtViewB = self.scrollView.frame;
rtViewB.origin.x = rtViewA.size.width;
rtViewB.origin.y = ;
UIView *viewB = [[UIView alloc]initWithFrame:rtViewB];
viewB.backgroundColor = [UIColor blueColor];
[self.scrollView addSubview:viewB];
}
再运行,结果如下:

视图错位的问题倒是没有了,可是竖直方向的滚动条还是在的呀!再打开Debug View Hierarchy,观察scroll view的位置是没问题了:

打印视图信息,发现新情况:scrollview的contentSize高度比frame高出64,viewA的也是:
Printing description of $:
<UIScrollView: 0x7ffc3400c600; frame = ( ; ); autoresize = W+H; gestureRecognizers = <NSArray: 0x7ffc33d036a0>; layer = <CALayer: 0x7ffc33c56100>; contentOffset: {, }; contentSize: {, }>
Printing description of $:
<UIView: 0x7ffc33c40650; frame = ( ; ); layer = <CALayer: 0x7ffc33c1c6b0>>
可是scroll view的contentSize跟它的frame的高度应该是一样的,为什么此时比它高了呢?肯定是frame的高度被调整过,而contentSize不知道,因此不会跟着改变。调整frame的只能是autolayout执行了约束,根据机型做了适配。我们必须在autolayout约束策略执行之后再去调整contentSize。应该在哪里做呢?文档里介绍:当view的bounds改变或者当视图改变子视图的位置,系统将调用viewDidLayoutSubviews函数,我们应该在这里调整scrollView.contentSize的大小。于是添加viewDidLayoutSubviews函数,如下:
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews]; CGRect rect = self.scrollView.frame;
rect.size.width *= ;
self.scrollView.contentSize = rect.size; CGRect rtViewA = self.scrollView.frame;
rtViewA.origin.x = ;
rtViewA.origin.y = ;
self.viewA.frame = rtViewA; CGRect rtViewB = self.scrollView.frame;
rtViewB.origin.x = rtViewB.size.width;
rtViewB.origin.y = ;
self.viewB.frame = rtViewB;
}
再次运行,终于正常了:

完美!示例代码可参见:https://github.com/palanceli/NavigationScrollView
UINavigationController + UIScrollView组合,视图尺寸的设置探秘(三)的更多相关文章
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(一)
UINavigationController和UIScrollView是iOS下几种主要的交互元素,但当我搭配二者在一起时,UIScrollView的滚动区域出现了很诡异的现象.我希望UIScroll ...
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(二)
承接上文,我想把view布局修改为如下模式,让ScrollView长在NavigationBar的下方,这总不会有遮挡的问题了吧: story board内容如下,主要是右侧视图蓝色区域添加了Scro ...
- bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月
问题 bootstrap-datetimepicker年视图中endDate设置结束时间为2016-08,(即8月之后的日期不能选)而在日历上显示时为2016-06,相差两个月,即6月之后的日期不能选 ...
- JAVA中常用需要设置的三个环境变量(JAVA_HOME、CLASSPATH、PATH)
JAVA中常用需要设置的三个环境变量: JAVA_HOME.CLASSPATH.PATH (一) 配置环境变量:(相对路径) 1. JAVA_HOME=x:/jdk1.6.0 2. 用%JAVA_HO ...
- Python+Selenium自动化-设置等待三种等待方法
Python+Selenium自动化-设置等待三种等待方法 如果遇到使用ajax加载的网页,页面元素可能不是同时加载出来的,这个时候,就需要我们通过设置一个等待条件,等待页面元素加载完成,避免出现 ...
- UIScrollView 滚动视图—IOS开发
转自:http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect ...
- UIScrollView滚动视图
一.基本知识 1.初始化 UIScrollView #import "ViewController.h" #define WIDTH[[UIScreen mainScreen]bo ...
- iOS开发中的错误整理,(百思项目,指示器位置)设置控件尺寸和点坐标,先设置尺寸,再设置点坐标
之前对控件的尺寸和点的坐标的设置从来都是想到什么写什么,从来没有关心过顺序.然后就有了这次的血的教训!!!!! 下面是错误的截图,先设置的中心点,然后设置的宽度.程序运行就这样了,点别的没有毛病!!! ...
- 改良UIScrollView滚动视图
#define HEIGHT self.view.frame.size.height #define WIDTH self.view.frame.size.width @interface V ...
随机推荐
- mezzanine安装配置
ubuntu 14.04 cd /var/wwwmkdir testingcd testingvirtualenv venv --python=python3. venv/bin/activate p ...
- 极路由U-boot解锁刷root固件教程,root后可刷华硕、如意云等多种固件,附赠全套刷软
9008正式版固件将会封堵此漏洞,想root的同学尽快了.安装新工具箱里的root保留,可升级官方最新固件并保留root. 此方法并非本人原创,只是将root的过程和经验做个总结,比较适合菜鸟做参考, ...
- Spring 自动注册及自动装配
Spring支持三种注册Bean及装配Bean的方式: 显式地在Java代码中注册及装配 显示地在Xml文件中注册及装配 隐式地装配,即自动注册及装配 这三种方式可以混合使用.选择哪种更多地是看个人品 ...
- Java-API-Package:java.util
ylbtech-Java-API-Package:java.util 1.返回顶部 1. java.util Interfaces Collection Comparator Deque Enumer ...
- leetcode590
树的后序遍历. class Solution { public: vector<Node> Tree; void postTree(Node node) { for (auto n : n ...
- Android webRTC 代码下载编译
1.安装depot tools git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git 然后把把depot ...
- intellij idea打包springboot项目
一.可执行jar包 注意点: maven的package类型需要为jar 配置了spring-boot-mavne-plugin插件 1.1.pom.xml <?xml version=&quo ...
- hadoop再次集群搭建(3)-如何选择相应的hadoop版本
之前接触过很多很多hadoop版本,现在重新搭建平台,面临选择哪个版本的问题. 当我们决定是否采用某个软件用于开源环境时,通常需要考虑以下几个因素: (1)是否为开源软件,即是否免费. (2) 是否有 ...
- http协议基础教程
引言 HTTP 是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和 扩展.目前在WWW中使用的是HTTP/ ...
- day36-hibernate检索和优化 09-Hibernate中的事务:事务处理