iOS 7 状态栏的半透明效果兼容旧版本操作系统

10

我在iOS 6中构建了一个具有半透明导航栏的应用程序。我想利用iOS 7中的半透明状态栏,并保持iOS 6中的应用程序不变,但是我的内容始终位于iOS 7中的状态栏下方,并且底部缺少20像素。我认为我可以编写非常繁琐的代码,检查设备是否具有iOS 7,然后相应地调整我的内容,但我担心这将是很多工作。

理想情况下,我希望为每个视图控制器的视图添加20像素的填充,以便内容向下移动,并在iOS 6上正常使用不透明的导航栏。

我已阅读有关主题的线程12,但提供的所有答案都没有解决我的问题。

请注意,我没有使用Interface Builder,并且所有VC都是通过编程方式创建的。


这个链接提供了关于这个问题的有趣信息:http://www.doubleencore.com/2013/09/developers-guide-to-the-ios-7-status-bar/ - lucasart
5个回答

6

5

以下是我为了始终将视图的顶部填充20像素(状态栏的高度)而采取的措施。

我在AppDelegate的application:didFinishLaunchingWithOptions方法中使用了以下代码:

...
// container holds my root view controller
UINavigationController *container = [UINavigationController alloc] init...];
...

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // iOS 7
    // Create parent controller that will contain your existing root view controller's view shifted 20 px down to account for the status bar.
    UIViewController *newRootController = [[UIViewController alloc] init];

    // Add my old root view controller as a child
    [newRootController addChildViewController:container];

    // Add its view as a subview
    [newRootController.view addSubview:container.view];

    // Call this method because it does some configuration?
    [container didMoveToParentViewController:newRootController];

    // Now just position the view starting at point (0, 20)
    UIView *aView = container.view;

    NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(aView);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[aView]|" options:0 metrics:nil views:viewDictionary];

    [newRootController.view addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aView]|" options:0 metrics:nil views:viewDictionary];

    [newRootController.view addConstraints:constraints];

    self.window.rootViewController = newRootController;
} else { // pre iOS 7
    self.window.rootViewController = container;
}

现在,无论何时您使用iOS 7,所有内容都将存在于根视图控制器的视图中,该视图向下移动了20个像素。您只需要在AppDelegate中执行一次即可。

我尝试了这个,但是收到了异常信息“无法同时满足约束条件”。 - D-Nice

5
你可以使用 Xcode 5 的 iOS6/7 delta 功能将所有视图设置为 -20,这会给你一个类似的体验。在 Interface Builder 中正确设置你的视图,并为 iOS6 支持使用 deltas。

xCode在哪里,我可以提及iOS6的delta? - Adil Malik
我必须提及所有子视图的delta-x和delta-height。然后它对我起作用了。 - Adil Malik
1
您可以使用command+a选择所有视图,并在deltas中设置相同的值。 - Léo Natan
他说他不使用Interface Builder来创建视图,所以我不确定这对他是否有效。 - bolnad

4
在info.plist中将“UIViewControllerBasedStatusBarAppearance”设置为NO(这样可以选择退出视图控制器调整状态栏样式,以便我们可以使用UIApplicationstatusBarStyle方法设置状态栏样式)。
在AppDelegate的application:didFinishLaunchingWithOptions方法中调用。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.clipsToBounds =YES;
    self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);

    //Added on 19th Sep 2013
    self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
return YES;

在构建iOS 6时对我没有起作用。此外,状态栏样式未找到。 - mhmdshawqi
这不适用于iOS 6。 - Hardik Thakkar

2

在iOS 7中,您可以通过设置以下内容来禁用视图进入顶部栏:

if([controller canPerformAction:@selector(setEdgesForExtendedLayout:) withSender:self]) {
        [controller setEdgesForExtendedLayout:UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接