使用UINavigationController后导致UIScollView尺寸变化
转自: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 文件中:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- self.window.backgroundColor = [UIColor whiteColor];
- //下面两行为增加的代码
- ViewController *rootViewController = [[ViewController alloc] init];
- [self.window setRootViewController:rootViewController];
- [self.window makeKeyAndVisible];
- return YES;
- }
2. 在 ViewController.m 中:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
- [scrollView setBackgroundColor:[UIColor redColor]];
- UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];
- [view setBackgroundColor:[UIColor blueColor]];
- [scrollView addSubview:view];
- [self.view addSubview:scrollView];
- }
3. 运行后的结果:
4. 现在使用 UINavigationController, 将开始 AppDelegate.m 增加的那两行代码修改成:
- ViewController *rootViewController = [[ViewController alloc] init];
- UINavigationController *navController = [[UINavigationController alloc]
- initWithRootViewController:rootViewController];
- [self.window setRootViewController:navController];
5. 现在再次运行程序:
如结果显示, scrollView 背景色为蓝色的子视图位置自动下移了。 而这个下移的距离刚好是 64.0px。
解决方法:
第一种:在 ViewController 的 init 的方法中增加一行代码:
- self.automaticallyAdjustsScrollViewInsets = NO;
第二种: 让UIScrollView 不要成为 ViewController 的 View 的第一个子视图。具体操作:将 viewDidLoad方法 修改成如下:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIView *firstSubView = [[UIView alloc] initWithFrame:self.view.bounds];
- [self.view addSubview:firstSubView];
- UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
- [scrollView setBackgroundColor:[UIColor redColor]];
- UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];
- [view setBackgroundColor:[UIColor blueColor]];
- [scrollView addSubview:view];
- [self.view addSubview:scrollView];
- }
第三种:将 UIScorllView 的子视图上移 64.0px 。修改 viewDidLoad 方法:
- UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
- [scrollView setBackgroundColor:[UIColor redColor]];
- CGRect viewFrame = CGRectMake(0, -64.0, CGRectGetWidth(scrollView.frame),
- CGRectGetHeight(scrollView.frame));
- UIView *view = [[UIView alloc] initWithFrame: viewFrame];
- [view setBackgroundColor:[UIColor blueColor]];
- [scrollView addSubview:view];
- [self.view addSubview:scrollView];
使用UINavigationController后导致UIScollView尺寸变化的更多相关文章
- SQL Server数据库账号密码变更后导致vCenter Server无法访问数据库
SQL Server数据库账号密码变更后导致vCenter Server无法访问数据库 1.1状况描述: 若SQL Server数据库的账号(这里以sa为例)密码发生了变更,那么连接数据的客户端vCe ...
- Centos7修改profile文件后导致vi command not find
Centos7修改profile文件后导致vi command not find,原因是profile文件没有配置正确,系统就无法找到精确命令了.解决方法: 1.在命令行中输入:export PATH ...
- 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案
更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...
- ride.py在运行python3.×版本后导致无法运行及解决办法
最近一直在自学python自动化,网上看到rf框架挺适合初学自动化测试,于是通过虫师的搭建了rf框架, 但是在使用过程中遇到了一个问题,在网上没有找到明确解决办法于是想到记录一下 之前为了搭建rf框架 ...
- macOS 升级后导致 dart bug
macOS 升级后导致 dart bug macOS 10.15.5 $ demo_app git:(master) flutter doctor # /Users/xgqfrms-mbp/Docum ...
- 子div设置float后导致父div无法自动撑开的问题
子div设置float后会导致父div无法自动撑开 原因:内部的DIV因为float:left之后,就丢失了clear:both和display:block的样式,所以外部的DIV不会被撑开. 以下是 ...
- ThinkPad E440 加内存后导致开不了机
上周五新买的ThinkPad E440,原装内存是4G DDR3 1600Hz,明显不够用,于是在京东上买了一根南亚易胜的4G DDR3 1600Hz.安装之后正常开机,明显感觉速度快了很多.可是用了 ...
- Android引用百度定位API第三方组件后导致其它.so文件无法正常加载的问题
查看当前调试设备CPU架构的方法: adb.exe shell getprop ro.product.cpu.abi (一般返回值为:armeabi-v7a) adb.exe shell getpr ...
- 关于Action中ValidateXXX方法校验一次失败后\导致以后一直返回input视图的情况
问题: 使用struts2的validateXXX()进行验证,出错后自动返回之前的画面,但是Form中的数据全部丢失了,如何才能保存出错前的Form是要解决的问题. 调查: 调查后发 ...
随机推荐
- android 如何连真机测试
1. 设置android手机为USB调试模式.步骤: menu---> 设置 ---> 应用程序 ---> 开发 , 选择[USB调试] 2. 用USB连接手机和电脑,并确保成功.步 ...
- 提高ASP.NET首页性能的十大方法
本文是我对ASP.NET页面载入速度提高的一些做法,这些做法分为以下部分: http://www.cnblogs.com/xiachufeng/archive/2011/11/09/2242130.h ...
- MISRA-C 2012 C90规范和C99规范对比
- spring常用注解以IOC理解
使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...
- Android Studio关于USB device not found的解决的方法
Android Studio关于USB device not found的解决的方法 我们使用Android Studio进行Android开发时.当我们使用真机进行调试时.非常可能会出现USB de ...
- glusterFS的部署流程
转自:http://www.cnblogs.com/terrycy/p/5915263.html GlusterFS简单配置 1.准备工作 准备三台机器(物理机或者虚拟机均可)用于安装和测试Glu ...
- 腾讯云-NGINX搭建静态网站
搭建静态网站 搭建Http静态服务器环境 任务时间:15min ~ 30min 搭建静态网站,首先需要部署环境.下面的步骤,将告诉大家如何在服务器上通过 Nginx 部署 HTTP 静态服务. 00. ...
- PHP-手册阅读笔记
1.第一次遇到$_ENV为空数组的情况, 原来是PHP.INI中variables_order为'GPCS'(表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER,只 ...
- MySQL-SQL语句中SELECT语句的执行顺序
SELECT语句的执行顺序大家比较少关注,下面将为您详细介绍SQL语句中SELECT语句的执行顺序,供您参考,希望对您能够有所帮助. SELECT语句的执行的逻辑查询处理步骤: (8)SELECT ( ...
- HDUOJ-----2399GPA
GPA Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...