使用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是要解决的问题. 调查: 调查后发 ...
随机推荐
- Javascript高级程序设计-对象
学习Javascript,最难的地方是什么? Object(对象)最难,初学者不容易掌握. Javascript是一种基于对象(object-based)的语言,它的语法中没有class(类). C# ...
- Oracle 之 表新增字段后修改字段顺序
工作中遇到:在为一个表新增字段后,新增字段在最后,想调整新增字段的位置. 1.原始方法: --新建临时表以存储正确的顺序 create table A_2 as select (column1,col ...
- 微信小程序 - 文字收缩与展开
wxml <view class='containers'> <text class="content {{ellipsis?'ellipsis':'unellipsis' ...
- mavern安装方法
Installation Instructions Maven is a Java tool, so you must have Java installed in order to proceed. ...
- LeetCode 37 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- quartz实现定时任务调度
一. 业务需求: 实际工作中我们一般会遇到这种需求: 使用Ajax技术每隔几秒从缓存或数据库中读取一些数据, 然后再显示在页面上, 眼下有一个比較好的定时调度框架: quartz能够满足我们的需求. ...
- 获取TrustedInstaller
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\runas] @="获取TrustedInstaller权限& ...
- 关于DES加密中的 DESede/CBC/PKCS5Padding
今天看到一段3DES加密算法的代码,用的参数是DESede/CBC/PKCS5Padding,感觉比较陌生,于是学习了一下. 遇到的java代码如下: Cipher cipher=Cipher.get ...
- uniq命令 (转)
uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用.也就是说,为了使uniq起作用,所有的重复行必须是相邻的. uniq语法 [root@www ~]# uniq [-icu] ...
- TestNG方法測试及注意要点 代码及配置具体解释(解决testng方法不运行问题)
教你解决为什么TestNG中方法加了@Test注解,也在配置文件里配置了,可是方法就是不运行! 在使用TestNG进行測试时,使用配置文件的方式更easy于维护.可是常常遇到明明方法写了也配置运行了, ...