首页 > 求解,ios7下 setNavigationBarHidden:YES 之后 页面会跟 Statusbar 叠在一起

求解,ios7下 setNavigationBarHidden:YES 之后 页面会跟 Statusbar 叠在一起

如图


我觉得这是iOS7运行时的问题。所有的view,不管是UINavigationController.view还是UIViewController.viewy值都是0,高度全屏幕(480或568)。不管你有没有NavigationBar,有没有StatusBar,有没有TabBar。 所以,如果非要修复这个问题,需要在viewDidLoad的时候判断view的高度,然后动态set到合适的y和height。


对,在iOS7中View的高度撑满了屏幕,包括NavBar和TabBar。


if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) self.edgesForExtendedLayout = UIRectEdgeNone;


ios7的屏幕坐标0,0点是从statusBar开始的,你可以再xib文件中,顶部留出20像数,然后控件的对齐方式为底部对齐。这样IOS6跟7都可以适应。这个是我能想到的相对简单的版本。如果还又其他的办法的话,可以讨论一下


这也是困扰我的问题之一。目前我想出了不用AutoLayout时的解决办法:如果用xib的话,请见http://blog.csdn.net/rhljiayou/article/details/12029927;如果不是用xib的话,只能手动偏移了。

#define IOS_7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
#define IOS6_7_DELTA(V,X,Y,W,H) if (IOS_7) {CGRect f = V.frame;f.origin.x += X;f.origin.y += Y;f.size.width += W;f.size.height += H;V.frame=f;}
...
IOS6_7_DELTA(view, 0, 20, 60, 0);

另外,如果使用了AutoLayout,并且用了StoryBoard,那么可以很方便地将搜索框跟TopLayoutGuide做一个constraint。这个TopLayoutGuide看起来是很有用的,可以看到官方文档里面说的用法。

  • If a status bar is visible onscreen and there is no visible navigation bar, the bottom of the status bar
  • If a navigation bar is visible onscreen, the bottom of the navigation bar
  • If there is no status bar or navigation bar visible onscreen, the top of the screen in the device’s current orientation

但是如果只是用xib的话,似乎没法在IB里面加这个constraint(这个问题见我在stackoverflow上问的问题,但是目前没有解答【大概我的英文太差了?】)。如果是纯代码的话,我对constraints语法不是很熟悉,写的估计是错的,仅供参考:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *verticalConstraint = @"V:|[v]";// for iOS6
    NSMutableDictionary *views = [NSMutableDictionary new];
    NSMutableArray *constraints = [NSMutableArray new];
    [views setObject:self.subview forKey:@"v"];
    if ([self respondsToSelector:@selector(topLayoutGuide)]) {
        // for iOS7
        [views setObject:self.topLayoutGuide forKey:@"topLayoutGuide"];
        verticalConstraint = @"V:[topLayoutGuide][v]";
    }
    [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraint options:0 metrics:nil views:views]];
    // ... other constraints
    [self.view addConstraints:constraints];
    // Do any additional setup after loading the view from its nib.
}

1.关于适配navigation

可以重写ViewController的基类,把对的navigation的高度进行判断,如果是iOS7一下nav的高度44,如果iOS7高度则设成64.

2.View的自适应高度

先定义一个iOS7self.view的高度为20,然后动态匹配就行

【热门文章】
【热门文章】