10

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

理想情况下,我想在每个视图控制器的视图顶部添加 20px 的填充,以便内容向下移动,并且在 iOS 6 上使用不透明的导航栏仍然可以正常工作。

我已经阅读了主题1 2上存在的线程,但所提供的答案都没有解决我的问题。

我应该注意,我没有使用 Interface Builder,我所有的 VC 都是以编程方式创建的。

4

5 回答 5

6

如果您正在使用auto layout,那么您需要做的就是Vertical Constraint从最顶部的视图添加一个,Top Layout Guide如下所示,它应该注意顶部间距。

在此处输入图像描述

欲了解更多信息: https ://developer.apple.com/library/ios/qa/qa1797/_index.html

于 2013-09-26T00:50:50.870 回答
5

您可以使用 iOS6/7 deltas 的新 Xcode 5 功能将所有视图设置为 -20,这将为您提供类似的体验。在界面构建器中为 iOS7 正确设置视图,并使用 deltas 来支持 iOS6。

于 2013-09-19T19:17:29.020 回答
5

这是我总是用 20px(状态栏的高度)填充视图顶部的方法。

我在 AppDelegate 的应用程序中使用了这段代码: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 中执行一次。

于 2013-09-19T19:56:30.213 回答
4

设置UIViewControllerBasedStatusBarAppearance' to NO in info.plist (To opt out of having view controllers adjust the status bar style so that we can set the status bar style by using theUIApplicationstatusBarStyle` 方法。)

在 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;
于 2013-09-25T09:05:16.867 回答
2

You can disable the view going under the top bar in ios 7 by setting the following:

if([controller canPerformAction:@selector(setEdgesForExtendedLayout:) withSender:self]) {
        [controller setEdgesForExtendedLayout:UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight];
}
于 2013-09-25T12:22:50.940 回答