Viewの余白とナビゲーションバーとTabbarの位置関係

3866 ワード

前言
iOS 7から、UIDIewControllerではデフォルトでフルスクリーンレイアウトが使用され、edgesForExtendedLayout、a u t o m a t i c a llyAdjustsScrollViewInsets、およびextendedLayoutIncludesOpaqueBarsの3つの属性が追加されてレイアウトを制御します.
1、edgesForExtendedLayout
このプロパティはデフォルトではUIrectEdgeAll(周囲に延びる)で、上部はナビゲーションバーで覆われ、下部はtabbarで覆われ、画面全体を占めています.
まず、何も設定されていない状況を見てみましょう.

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"test";
    self.view.backgroundColor = [UIColor orangeColor];
    
}```

![     ](http://upload-images.jianshu.io/upload_images/2498906-3a8fffdad0543ce8.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)


  , edgesForExtendedLayout     UIRectEdgeNone,      tabbar    
  • (void)viewDidLoad { [super viewDidLoad]; self.title = @"test"; self.view.backgroundColor = [UIColor orangeColor]; self.edgesForExtendedLayout = UIRectEdgeNone;

  • }
    
    ![UIRectEdgeNone](http://upload-images.jianshu.io/upload_images/2498906-9bb7401c1961b65e.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
    
              tabbar       ,       tabbar       ,  edgesForExtendedLayout     UIRectEdgeAll,view          ,     tabbar  。    :
    
  • (void)viewDidLoad { [super viewDidLoad]; self.title = @"test"; self.view.backgroundColor = [UIColor orangeColor]; self.edgesForExtendedLayout = UIRectEdgeAll; self.navigationController.navigationBar.translucent = NO; self.tabBarController.tabBar.translucent = NO; }
  • #2、extendedLayoutIncludesOpaqueBars
                   ,     tabbar    ,view      ,    extendedLayoutIncludesOpaqueBars YES,   view        ,                    ,    ,        NO    :
    
  • (void)viewDidLoad { [super viewDidLoad]; self.title = @"test"; self.view.backgroundColor = [UIColor orangeColor]; UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; greenView.backgroundColor = [UIColor greenColor]; [self.view addSubview:greenView]; self.navigationController.navigationBar.translucent = NO; self.tabBarController.tabBar.translucent = NO; NSLog(@"%s%@",func,NSStringFromCGRect(self.view.frame));

  • }
    -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated];
    NSLog(@"%s%@",__func__,NSStringFromCGRect(self.view.frame));
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-150, 150, 150)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    

    }
    
    ![  view frame](http://upload-images.jianshu.io/upload_images/2498906-266fa8e189e8fa72.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)
             ,view     viewDidAppear        ,         viewDidAppear       ,     :
    
    ![extendedLayoutIncludesOpaqueBars NO](http://upload-images.jianshu.io/upload_images/2498906-3a81ddea95fffb28.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
    
        viewDidLoad         ,    :
    

    self.extendedLayoutIncludesOpaqueBars = YES;
    
    ![extendedLayoutIncludesOpaqueBars YES](http://upload-images.jianshu.io/upload_images/2498906-93070749d7cbc646.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
         ,           
    
    #3、automaticallyAdjustsScrollViewInsets
              UIScrollView     (  UITabbleView) ,             , automaticallyAdjustsScrollViewInsets   YES(   YES) ,            ,            ,      ,                ;  , automaticallyAdjustsScrollViewInsets   NO ,                  ,     :
    ![automaticallyAdjustsScrollViewInsets YES](http://upload-images.jianshu.io/upload_images/2498906-080abedca4c5c496.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
    
    ![automaticallyAdjustsScrollViewInsets NO ](http://upload-images.jianshu.io/upload_images/2498906-fdfaafcd18b904ed.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
              ,