zoukankan      html  css  js  c++  java
  • 使用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 文件中:

    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.  
    9. [self.window makeKeyAndVisible];
    10. return YES;
    11. }

      

      

       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.  
    5. UIView *firstSubView = [[UIView alloc] initWithFrame:self.view.bounds];
    6. [self.view addSubview:firstSubView];
    7. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(30.0, 64.0, 260.0, 300.0)];
    8. [scrollView setBackgroundColor:[UIColor redColor]];
    9. UIView *view = [[UIView alloc] initWithFrame:scrollView.bounds];
    10. [view setBackgroundColor:[UIColor blueColor]];
    11. [scrollView addSubview:view];
    12. [self.view addSubview:scrollView];
    13. }

       

        第三种:将 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];

     
     

  • 相关阅读:
    Code First 迁移----官方 应用程序启动时自动升级(MigrateDatabaseToLatestVersion 初始值设定项)
    使用 Entity Framework Core 时,通过代码自动 Migration
    泛型反射获取特性值
    使用Expression动态创建lambda表达式
    ElasticSearch + Logstash + Kibana 搭建笔记
    PHP获取指定函数定义在哪个文件中及行号
    Wordpress中文章的特色图像Featured Image究竟存在哪里?
    PhpSpreadsheet生成Excel时实现单元格自动换行
    Composer使用体验
    PHP命名空间学习笔记
  • 原文地址:https://www.cnblogs.com/wangpei/p/3833188.html
Copyright © 2011-2022 走看看