转自: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. android 如何连真机测试

    1. 设置android手机为USB调试模式.步骤: menu---> 设置 ---> 应用程序 ---> 开发 , 选择[USB调试] 2. 用USB连接手机和电脑,并确保成功.步 ...

  2. 提高ASP.NET首页性能的十大方法

    本文是我对ASP.NET页面载入速度提高的一些做法,这些做法分为以下部分: http://www.cnblogs.com/xiachufeng/archive/2011/11/09/2242130.h ...

  3. MISRA-C 2012 C90规范和C99规范对比

  4. spring常用注解以IOC理解

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  5. Android Studio关于USB device not found的解决的方法

    Android Studio关于USB device not found的解决的方法 我们使用Android Studio进行Android开发时.当我们使用真机进行调试时.非常可能会出现USB de ...

  6. glusterFS的部署流程

    转自:http://www.cnblogs.com/terrycy/p/5915263.html GlusterFS简单配置   1.准备工作 准备三台机器(物理机或者虚拟机均可)用于安装和测试Glu ...

  7. 腾讯云-NGINX搭建静态网站

    搭建静态网站 搭建Http静态服务器环境 任务时间:15min ~ 30min 搭建静态网站,首先需要部署环境.下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务. 00. ...

  8. PHP-手册阅读笔记

    1.第一次遇到$_ENV为空数组的情况, 原来是PHP.INI中variables_order为'GPCS'(表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER,只 ...

  9. MySQL-SQL语句中SELECT语句的执行顺序

    SELECT语句的执行顺序大家比较少关注,下面将为您详细介绍SQL语句中SELECT语句的执行顺序,供您参考,希望对您能够有所帮助. SELECT语句的执行的逻辑查询处理步骤: (8)SELECT ( ...

  10. HDUOJ-----2399GPA

    GPA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...