怎么让self.view的Y从navigationBar下面开始计算

原文地址 http://blog.sina.com.cn/s/blog_1410870560102wu9a.htmlhtml

在iOS 7中,苹果引入了一个新的属性,叫作[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为何全部的UI元素都往上漂移了44pt。有时会加上顶部tool bar的高度 20, 20+44 = 64。并且下面tabbar也缩进的49布局

解决办法

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view.  
    if (OSVersionIsAtLeastiOS7()) {  
        if ([self respondsToSelector:@selector(edgesForExtendedLayout)])  
        {  
            self.edgesForExtendedLayout = UIRectEdgeNone;  
        }  
    }  
}
  • 若是设置成 self.edgesForExtendedLayout = UIRectEdgeBottom;那么就会self.view.frame是从navigationBar下面开始计算一直到屏幕底部;
  • 若是设置成 self.edgesForExtendedLayout = UIRectEdgeNone;那么就会self.view.frame是从navigationBar下面开始计算一直到屏幕tabBar上部;
  • 若是设置成 self.edgesForExtendedLayout = UIRectEdgeTop;那么就会self.view.frame是从navigationBar上面计算面开始计算一直到屏幕tabBar上部;

了解更多

  • iOS7以上系统,self.navigationController.navigationBar.translucent默认为YES,self.view.frame.origin.y从0开始(屏幕最上部)。
  • 此时如果添加代码self.edgesForExtendedLayout = UIRectEdgeNone(iOS7.0之后方法);self.view.frame.origin.y会下移64像素至navBar下方开始。可是此时当push到一个新的controller以前给当前屏幕截图的时候[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];,因为navBar是透明的,会出现64像素的黑色区域。
  • 此时只须要把self.navigationController.navigationBar.translucent=NO便可,不须要加代码self.edgesForExtendedLayout;code

  • iOS7以后也增长了一个self.tabBarController.tabBar.translucent的属性,默认为YES。当应用同时使用navBar和TabBar的时候。设置self.tabBarController.tabBar.translucent=NO而且self.navigationController.navigationBar.translucent=NO时候,获得self.view.frame--->{{0, 64}, {320, 455}}。视图的高度也改变为navBar和tabBar之间的455像素。当self.navigationController.navigationBar.translucent=YES而且self.tabBarController.tabBar.translucent=NO的时候self.view.frame--->{{0, 0}, {320, 519}};其都为YES的时候self.view.frame--->{{0, 0}, {320, 568}};htm

注意:设置self.edgesForExtendedLayout = UIRectEdgeNone;的时候会使得navBar和tabBar都不占空间。self.view.frame--->{{0, 64}, {320, 455}}。此时iOS7默认navBar和tabBar都是透明的。截图的时候须要注意。blog

相关文章
相关标签/搜索