xcode 具有向后兼容性的 iOS 7 状态栏半透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18902963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:49:57  来源:igfitidea点击:

iOS 7 status bar translucency with backwards compatibility

iosobjective-cxcodeios6ios7

提问by D-Nice

I built my app to have a translucent navbar in iOS 6. I'd like to utilize the translucent status bar in iOS 7 and keep the app as is in iOS 6, but my content is always underneath the status bar in iOS 7, with 20px missing on the bottom. I figure that I can make the very tedious code changes that check whether the device has iOS 7 and then adjust my content accordingly, but I fear that this is going to be a lot of work.

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

Ideally, I'd like to add 20px of padding to the top of every view controller's view, so that the content shifts down, and still functions fine with an opaque navbar on iOS 6.

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

I've read the threads that exist on the subject 12, but none of the answers provided solved my problem.

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

I should note that I am NOT using Interface Builder and all my VCs are being created programmatically.

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

回答by Yas T.

If you are using auto layout, then all you need to do is add a Vertical Constraintfrom your top most view to Top Layout Guideas shown below and it should take care the top spacing.

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

enter image description here

在此处输入图片说明

For more info: https://developer.apple.com/library/ios/qa/qa1797/_index.html

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

回答by Leo Natan

You can use the new Xcode 5 feature of iOS6/7 deltas to set -20 to all your view, which will give you a similar experience. Set your views correctly for iOS7 in interface builder, and use deltas for iOS6 support.

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

回答by Matt Tang

Here's what I did to always pad the top of my view with 20px (height of the status bar).

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

I used this code in my AppDelegate's application:didFinishLaunchingWithOptions: method

我在 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;
}

Now whenever you're in iOS 7, everything will exist in the root view controller's view that is shifted 20 pixels down. You'll only have to do this once in your AppDelegate.

现在,无论何时在 iOS 7 中,所有内容都将存在于下移 20 个像素的根视图控制器的视图中。您只需在 AppDelegate 中执行此操作一次。

回答by Hardik Thakkar

Set 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` method.)

设置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` 方法。)

In AppDelegate's application:didFinishLaunchingWithOptions, call

在 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;

回答by AZ0

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

您可以通过设置以下内容来禁用 ios 7 顶栏下方的视图:

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