还是在苹果的 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组合,视图尺寸的设置探秘(三)的更多相关文章

  1. UINavigationController + UIScrollView组合,视图尺寸的设置探秘(一)

    UINavigationController和UIScrollView是iOS下几种主要的交互元素,但当我搭配二者在一起时,UIScrollView的滚动区域出现了很诡异的现象.我希望UIScroll ...

  2. UINavigationController + UIScrollView组合,视图尺寸的设置探秘(二)

    承接上文,我想把view布局修改为如下模式,让ScrollView长在NavigationBar的下方,这总不会有遮挡的问题了吧: story board内容如下,主要是右侧视图蓝色区域添加了Scro ...

  3. bootstrap-datetimepicker年视图中endDate设置之后比正常时间提前两个月

    问题 bootstrap-datetimepicker年视图中endDate设置结束时间为2016-08,(即8月之后的日期不能选)而在日历上显示时为2016-06,相差两个月,即6月之后的日期不能选 ...

  4. JAVA中常用需要设置的三个环境变量(JAVA_HOME、CLASSPATH、PATH)

    JAVA中常用需要设置的三个环境变量: JAVA_HOME.CLASSPATH.PATH (一) 配置环境变量:(相对路径) 1. JAVA_HOME=x:/jdk1.6.0 2. 用%JAVA_HO ...

  5. Python+Selenium自动化-设置等待三种等待方法

    Python+Selenium自动化-设置等待三种等待方法   如果遇到使用ajax加载的网页,页面元素可能不是同时加载出来的,这个时候,就需要我们通过设置一个等待条件,等待页面元素加载完成,避免出现 ...

  6. UIScrollView 滚动视图—IOS开发

    转自:http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect  ...

  7. UIScrollView滚动视图

    一.基本知识 1.初始化 UIScrollView #import "ViewController.h" #define WIDTH[[UIScreen mainScreen]bo ...

  8. iOS开发中的错误整理,(百思项目,指示器位置)设置控件尺寸和点坐标,先设置尺寸,再设置点坐标

    之前对控件的尺寸和点的坐标的设置从来都是想到什么写什么,从来没有关心过顺序.然后就有了这次的血的教训!!!!! 下面是错误的截图,先设置的中心点,然后设置的宽度.程序运行就这样了,点别的没有毛病!!! ...

  9. 改良UIScrollView滚动视图

    #define HEIGHT  self.view.frame.size.height #define WIDTH    self.view.frame.size.width @interface V ...

随机推荐

  1. YUV转换成RGB算法

    YUV转换成RGB void yuvtorgb ( double *rgb,unsigned char *yuv) { int i; rgb[] = ] + + ] - ); // r rgb[] = ...

  2. MD5加密 及获得密码盐

    MD5加密 及获得密码盐 using System; using System.Collections.Generic; using System.Configuration; using Syste ...

  3. JDK7和JDK8新特性

    参考:http://www.cnblogs.com/langtianya/p/3757993.html JDK 1.7 新特性 1,switch中可以使用字串了 String s = "te ...

  4. HDU3887(树dfs序列+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  5. (转)AppCan中调用系统浏览器打开网页

    <!DOCTYPE html> <html> <head> <style>body{ background:#fff; font-size:30px;} ...

  6. Day3-Python基础3--局部变量和全局变量

    一.局部变量 def test(name): print("before change:",name) name = "maqing" #局部变量name,只能 ...

  7. 线程及同步的性能 – 线程池/ ThreadPoolExecutors/ ForkJoinPool

    线程池和ThreadPoolExecutors 虽然在程序中可以直接使用Thread类型来进行线程操作,但是更多的情况是使用线程池,尤其是在Java EE应用服务器中,一般会使用若干个线程池来处理来自 ...

  8. binlog之三:binlog开启、查看

    0.开启二进制日志记录功能:        #vim /etc/my.cnf            [mysqld]            log_bin=mysql-bin            b ...

  9. MySQL 更新失效

    create table t(id int not null PRIMARY key,c int default null) engine=innodb;insert into t(id,c)valu ...

  10. 转:InnoDB多版本(MVCC)实现简要分析

    InnoDB多版本(MVCC)实现简要分析 基本知识 假设对于多版本(MVCC)的基础知识,有所了解.InnoDB为了实现多版本的一致读,采用的是基于回滚段的协议. 行结构 InnoDB表数据的组织方 ...