在iOS 7中,UIStatusBar被设计成这样,它与视图合并:

(GUI由Tina tavvar设计)

It is cool, but it will somewhat mess up your view when you have something at the top part of your view, and it becomes overlapped with the status bar. Is there a simple solution (such as setting a property in info.plist) that can change the way it works [not overlapping] back to how it is in iOS6? I know a more straightforward solution is to have self.view.center.x + 20 points for every single view controller, but changing them will screw other dimensions up (having a different self.view.center.x can cause problem to custom segues, etc.) and suddenly it turns into a tedious job that is best to be avoided. I'll really be glad if someone can provide me an one-liner solution for this.

附注:我知道我可以隐藏状态栏

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

在didFinishLaunchingWithOptions方法中,但这是一个变通方法,一个避免问题的快捷方式,所以我不认为这是一个真正的解决方案。


当前回答

我发现这里是这个导航栏问题在iOS7的最佳替代方案和解决方案!!

http://www.appcoda.com/customize-navigation-status-bar-ios-7/

我希望它能消除我们所有的疑问和担忧。

其他回答

你可以一起隐藏状态栏。所以你的应用会是全屏的。我想这是你能得到的最好的了。

UIStatusBarStyleNone或目标设置。

我在iOS 7中实现了iOS 6的状态栏。

在info.plist中设置UIViewControllerBasedStatusBarAppearance为NO

(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法

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

    //Added on 19th Sep 2013
    NSLog(@"%f",self.window.frame.size.height);
    self.window.bounds = CGRectMake(0,0, self.window.frame.size.width, self.window.frame.size.height);
}

它可能会把你所有的视图都压低20个像素。要克服这一点,请使用以下代码在-(void)viewDidAppear:(BOOL)animated方法

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    CGRect frame=self.view.frame;
    if (frame.size.height==[[NSUserDefaults standardUserDefaults] floatForKey:@"windowHeight"])
    {
        frame.size.height-=20;
    }
    self.view.frame=frame;
}

你必须在didFinishLauncing Method中设置窗口分配后的windowHeight Userdefaults值

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[NSUserDefaults standardUserDefaults] setFloat:self.window.frame.size.height forKey:@"windowHeight"];

如果你正在使用接口构建器,试试这个:

在你的xib文件中:

1)选择主视图,设置背景颜色为黑色(或任何你想要状态栏的颜色)

2) Make sure the background is a self contained subview positioned as a top level child of the controller's view. Move your background to become a direct child of the controller's view. Check the autosizing panel to be sure that you've locked all frame edges, activated both flexibility axes, and if this is a UIImageView, set the content mode to Scale to fill. Programmatically this translates to contentMode set to UIViewContentModeScaleToFill and has its auto resizing mask set to (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight).

3)现在将所有被锁定的东西上下移动20点,并设置iOS 6/7 delta Y为-20。 在自动缩放面板中,所有被锁定在顶部框架上的顶级子图层都需要向下移动20点,并将其iOS 6/7 delta Y设置为-20。(Cmd选择所有这些,并按下箭头20次-有更好的方法吗?)

4) Adjust the iOS 6/7 delta height of all of the above items that had a flexible height. Any of the items that were locked to the frame top and bottom and had flexible height enabled in the autosizing panel must also have their iOS 6/7 delta height set to 20. That includes the background view mentioned above. This may seem anti-intuitive, but due to the order in which these are applied, it is necessary. The frame height is set first (based on device), then the deltas are applied, and finally the autosizing masks are applied based upon the offset positions of all of the child frames - think it through for a bit, it will make sense.

5)最后,被锁定在底部框架而不是顶部框架的项目根本不需要delta。

这将给你在iOS7和iOS6中相同的状态栏。

另一方面,如果你想要iOS7样式同时保持iOS6兼容性,那么将背景视图的delta Y / delta height值设置为0。

要查看更多iOS7迁移信息,请阅读全文:http://uncompiled.blogspot.com/2013/09/legacy-compatible-offsets-in-ios7.html

2013年9月19日更新: 固定缩放bug添加 self.window.bounds = CGRectMake(0,20, self.window.frame.size.width, self.window.frame.size.height); 纠正了NSNotificationCenter语句中的错别字

2013年9月12日更新: 修正了UIViewControllerBasedStatusBarAppearance为NO 增加了一个解决方案的应用程序与屏幕旋转 增加了一个改变状态栏背景颜色的方法。

显然,没有办法将iOS7的状态栏恢复到iOS6的工作状态。

然而,我们总是可以写一些代码,把状态栏变成像ios6一样,这是我能想到的最短的方法:

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 the UIApplicationstatusBarStyle method.) In AppDelegate's application:didFinishLaunchingWithOptions, call if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { [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 7。 设置状态栏的内容为白色,而不是UIStatusBarStyleDefault。 避免那些超出可见边界的子视图出现(对于从顶部进入主视图的动画视图)。 通过移动和调整应用程序的窗口框架,创造一种状态栏占用空间的错觉,就像在iOS 6中一样。

对于具有屏幕旋转功能的应用程序,

使用NSNotificationCenter通过添加来检测方向变化

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidChangeStatusBarOrientation:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

在if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1),并在AppDelegate中创建一个新方法:

- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
    int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
    int w = [[UIScreen mainScreen] bounds].size.width;
    int h = [[UIScreen mainScreen] bounds].size.height;
    switch(a){
        case 4:
            self.window.frame =  CGRectMake(0,20,w,h);
            break;
        case 3:
            self.window.frame =  CGRectMake(-20,0,w-20,h+20);
            break;
        case 2:
            self.window.frame =  CGRectMake(0,-20,w,h);
            break;
        case 1:
           self.window.frame =  CGRectMake(20,0,w-20,h+20);
    }
}

因此,当方向改变时,它将触发一个switch语句来检测应用程序的屏幕方向(纵向,倒置,横向左,或横向右),并分别改变应用程序的窗口框架,以创建iOS 6状态栏错觉。

更改状态栏的背景颜色:

Add

 @property (retain, nonatomic) UIWindow *background;

在AppDelegate.h中使背景成为类中的一个属性,并防止ARC释放它。(如果你不使用ARC,你不必这样做。)

之后,你只需要在if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)中创建UIWindow:

background = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
background.backgroundColor =[UIColor redColor];
[background setHidden:NO];

别忘了@synthesize background;@implementation AppDelegate!

更新(新解决方案) 本次更新是iOS 7导航栏问题的最佳解决方案。您可以设置导航栏颜色示例:FakeNavBar。backgroundColor = [UIColor redColor]; 注意:如果你使用默认的导航控制器,请使用旧的解决方案。 AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    if(NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0)
    {
        UIView *FakeNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
        FakeNavBar.backgroundColor = [UIColor whiteColor];

        float navBarHeight = 20.0;
        for (UIView *subView in self.window.subviews) {

            if ([subView isKindOfClass:[UIScrollView class]]) {
                subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height - navBarHeight);
            } else {
                subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height);
            }
        }
        [self.window addSubview:FakeNavBar];
    }

    return YES;

}

旧的解决方案-如果你使用以前的代码,请忽略下面的代码和图像 这是旧版本的iOS 7导航栏解决方案。

我用以下代码解决了这个问题。这是用来添加状态栏的。 didFinishLaunchingWithOptions

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    UIView *addStatusBar = [[UIView alloc] init];
    addStatusBar.frame = CGRectMake(0, 0, 320, 20);
    addStatusBar.backgroundColor = [UIColor colorWithRed:0.973 green:0.973 blue:0.973 alpha:1]; //change this to match your navigation bar
    [self.window.rootViewController.view addSubview:addStatusBar];
}

对于界面构建器,这是在iOS 6中打开的;它从0像素开始。

注意:iOS 6/7只有在详细信息窗格的“文件检查器”(最左边的图标)中取消视图控制器的“使用自动布局”时才会出现增量。