转自:http://www.w3c.com.cn/%E4%BD%BF%E7%94%A8uinavigationcontroller%E5%90%8E%E5%AF%BC%E8%87%B4uiscollview%E5%B0%BA%E5%AF%B8%E5%8F%98%E5%8C%96

http://blog.csdn.net/b719426297/article/details/22290257

在 iOS 7 中,如果某个 UIViewController 的 self.view 第一个子视图是 UIScollView, 同时当这个 UIViewController 被 push 或 initWithRootController 成为 UINavigationController控制的Controller时,这个 UIViewController的 view 的子视图 UIScollView 的所有子视图, 都会被下移 64px。

这个下移 64px 的前提是 navigationBar 和 statusBar 没有隐藏。因为为 statusBar 默认的 Height 是 20px,而 navigatiBar  默认的 Height 是 44px。

实例:

1. 在 AppDelegate.m 文件中:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. self.window.backgroundColor = [UIColor whiteColor];
  5. //下面两行为增加的代码
  6. ViewController *rootViewController = [[ViewController alloc] init];
  7. [self.window setRootViewController:rootViewController];
  8. [self.window makeKeyAndVisible];
  9. return YES;
  10. }

2. 在 ViewController.m 中:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
  5. [scrollView setBackgroundColor:[UIColor redColor]];
  6. UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];
  7. [view setBackgroundColor:[UIColor blueColor]];
  8. [scrollView addSubview:view];
  9. [self.view addSubview:scrollView];
  10. }

3. 运行后的结果:

     
   4. 现在使用 UINavigationController,  将开始 AppDelegate.m 增加的那两行代码修改成:

  1. ViewController *rootViewController = [[ViewController alloc] init];
  2. UINavigationController *navController = [[UINavigationController alloc]
  3. initWithRootViewController:rootViewController];
  4. [self.window setRootViewController:navController];

5. 现在再次运行程序:

如结果显示, scrollView 背景色为蓝色的子视图位置自动下移了。 而这个下移的距离刚好是 64.0px。

解决方法:

第一种:在 ViewController 的 init 的方法中增加一行代码:

  1. self.automaticallyAdjustsScrollViewInsets = NO;

第二种: 让UIScrollView 不要成为 ViewController 的 View 的第一个子视图。具体操作:将 viewDidLoad方法 修改成如下:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UIView *firstSubView = [[UIView alloc] initWithFrame:self.view.bounds];
  5. [self.view addSubview:firstSubView];
  6. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
  7. [scrollView setBackgroundColor:[UIColor redColor]];
  8. UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];
  9. [view setBackgroundColor:[UIColor blueColor]];
  10. [scrollView addSubview:view];
  11. [self.view addSubview:scrollView];
  12. }

第三种:将 UIScorllView 的子视图上移 64.0px 。修改 viewDidLoad 方法:

  1. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
  2. [scrollView setBackgroundColor:[UIColor redColor]];
  3. CGRect viewFrame = CGRectMake(0, -64.0, CGRectGetWidth(scrollView.frame),
  4. CGRectGetHeight(scrollView.frame));
  5. UIView *view = [[UIView alloc] initWithFrame: viewFrame];
  6. [view setBackgroundColor:[UIColor blueColor]];
  7. [scrollView addSubview:view];
  8. [self.view addSubview:scrollView];

使用UINavigationController后导致UIScollView尺寸变化的更多相关文章

  1. SQL Server数据库账号密码变更后导致vCenter Server无法访问数据库

    SQL Server数据库账号密码变更后导致vCenter Server无法访问数据库 1.1状况描述: 若SQL Server数据库的账号(这里以sa为例)密码发生了变更,那么连接数据的客户端vCe ...

  2. Centos7修改profile文件后导致vi command not find

    Centos7修改profile文件后导致vi command not find,原因是profile文件没有配置正确,系统就无法找到精确命令了.解决方法: 1.在命令行中输入:export PATH ...

  3. 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案

    更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...

  4. ride.py在运行python3.×版本后导致无法运行及解决办法

    最近一直在自学python自动化,网上看到rf框架挺适合初学自动化测试,于是通过虫师的搭建了rf框架, 但是在使用过程中遇到了一个问题,在网上没有找到明确解决办法于是想到记录一下 之前为了搭建rf框架 ...

  5. macOS 升级后导致 dart bug

    macOS 升级后导致 dart bug macOS 10.15.5 $ demo_app git:(master) flutter doctor # /Users/xgqfrms-mbp/Docum ...

  6. 子div设置float后导致父div无法自动撑开的问题

    子div设置float后会导致父div无法自动撑开 原因:内部的DIV因为float:left之后,就丢失了clear:both和display:block的样式,所以外部的DIV不会被撑开. 以下是 ...

  7. ThinkPad E440 加内存后导致开不了机

    上周五新买的ThinkPad E440,原装内存是4G DDR3 1600Hz,明显不够用,于是在京东上买了一根南亚易胜的4G DDR3 1600Hz.安装之后正常开机,明显感觉速度快了很多.可是用了 ...

  8. Android引用百度定位API第三方组件后导致其它.so文件无法正常加载的问题

    查看当前调试设备CPU架构的方法: adb.exe shell getprop ro.product.cpu.abi  (一般返回值为:armeabi-v7a) adb.exe shell getpr ...

  9. 关于Action中ValidateXXX方法校验一次失败后\导致以后一直返回input视图的情况

    问题:     使用struts2的validateXXX()进行验证,出错后自动返回之前的画面,但是Form中的数据全部丢失了,如何才能保存出错前的Form是要解决的问题. 调查:     调查后发 ...

随机推荐

  1. Javascript高级程序设计-对象

    学习Javascript,最难的地方是什么? Object(对象)最难,初学者不容易掌握. Javascript是一种基于对象(object-based)的语言,它的语法中没有class(类). C# ...

  2. Oracle 之 表新增字段后修改字段顺序

    工作中遇到:在为一个表新增字段后,新增字段在最后,想调整新增字段的位置. 1.原始方法: --新建临时表以存储正确的顺序 create table A_2 as select (column1,col ...

  3. 微信小程序 - 文字收缩与展开

    wxml <view class='containers'> <text class="content {{ellipsis?'ellipsis':'unellipsis' ...

  4. mavern安装方法

    Installation Instructions Maven is a Java tool, so you must have Java installed in order to proceed. ...

  5. LeetCode 37 Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...

  6. quartz实现定时任务调度

    一. 业务需求: 实际工作中我们一般会遇到这种需求: 使用Ajax技术每隔几秒从缓存或数据库中读取一些数据, 然后再显示在页面上, 眼下有一个比較好的定时调度框架: quartz能够满足我们的需求. ...

  7. 获取TrustedInstaller

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\runas] @="获取TrustedInstaller权限& ...

  8. 关于DES加密中的 DESede/CBC/PKCS5Padding

    今天看到一段3DES加密算法的代码,用的参数是DESede/CBC/PKCS5Padding,感觉比较陌生,于是学习了一下. 遇到的java代码如下: Cipher cipher=Cipher.get ...

  9. uniq命令 (转)

    uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [-icu] ...

  10. TestNG方法測试及注意要点 代码及配置具体解释(解决testng方法不运行问题)

    教你解决为什么TestNG中方法加了@Test注解,也在配置文件里配置了,可是方法就是不运行! 在使用TestNG进行測试时,使用配置文件的方式更easy于维护.可是常常遇到明明方法写了也配置运行了, ...