在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方法中,但这是一个变通方法,一个避免问题的快捷方式,所以我不认为这是一个真正的解决方案。


当前回答

我在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"];

其他回答

我已经看了很多很多很多教程来解决这个该死的问题。但是没有一个有用!以下是我的解决方案,对我来说很管用:

if( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f ) {
    float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    for( UIView *v in [self.view subviews] ) {
        CGRect rect = v.frame;
        rect.origin.y += statusBarHeight;
        v.frame = rect;
    }
}

逻辑很简单。我改变了所有孩子对自我的看法。视图为20像素。 这是所有。然后,屏幕截图就会像iOS 6一样显示出来。我讨厌iOS7的状态栏!~ " ~

下面是广泛使用Storyboard的项目的另一种方法:

目标:

这种方法的目标是在iOS7中重新创建与在iOS6中相同的状态栏样式(见问题标题“iOS7状态栏回到iOS6样式?”)。

简介:

为了实现这一点,我们尽可能多地使用故事板,通过向下移动状态栏重叠的UI元素(在iOS 7下),同时使用增量来恢复iOS 6.1或更早版本的向下布局更改。在ios7中产生的额外空间然后被一个UIView占用,并将backgroundColor设置为我们选择的颜色。后者可以在代码中创建,也可以使用故事板(参见下面的替代方案)

假设:

为了在执行以下步骤时获得所需的结果,假设基于视图控制器的状态栏外观被设置为NO,并且您的状态栏样式被设置为“透明黑色样式(alpha of 0.5)”或“不透明黑色样式”。这两种设置都可以在项目设置的“信息”下找到或添加。

步骤:

Add a subview to the UIWindow to serve as your status bar background. To achieve this, add the following to your AppDelegate's application: didFinishLaunchingWithOptions: after makeKeyAndVisible if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) { UIView *statusBarBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, yourAppsUIWindow.frame.size.width, 20)]; statusBarBackgroundView.backgroundColor = [UIColor blackColor]; [yourAppsUIWindow addSubview:statusBarBackgroundView]; } Since you programmatically added a background for iOS 7 ONLY, you will have to adjust the layout of your UI elements that are overlapped by the status bar accordingly whilst preserving their layout for iOS6. To achieve this, do the following: Ensure that Use Autolayout is unchecked for your Storyboard (this is because otherwise "iOS 6/7 Deltas" is not shown in the Size Inspector). To do this: select your Storyboard file show Utilities select "Show the File Inspector" Under "Interface Builder Document" uncheck "Use Autolayout" Optionally, to help you monitor the layout changes for both iOS 7 AND 6 as you apply them, select the "Assistant Editor", select "Preview" and "iOS 6.1 or earlier": Now select the UI element you want to adjust so it isn't overlapped by the status bar anymore Select "Show the Size Inspector" in the Utilities column Reposition your UI element along the Y-axis by the same amount as the statusbar bg height: And change the iOS6/7 Deltas value for Y by the same NEGATIVE amount as the statusbar bg height (Note the change in the iOS 6 preview if you're using it):

选择:

为了在故事板繁重的项目中添加更少的代码,并让状态栏背景自动旋转,而不是以编程方式为状态栏添加背景,你可以为每个视图控制器添加一个彩色视图,位于所称视图控制器的主视图的最顶部。然后你将改变这个新视图的高度delta为与你的视图高度相同的负数(使它在iOS 6下消失)。

这个替代方案的缺点(尽管考虑到自旋转兼容性可能可以忽略不计)是,如果你在iOS 6中查看你的故事板,这个额外的视图不会立即可见。如果你看了一下故事板的“文档大纲”,你就会知道它在那里。

试试这个简单的方法....

步骤1:在单个viewController中更改

[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];

步骤2:更改整个应用程序

info.plist
      ----> Status Bar Style
                  --->UIStatusBarStyle to UIStatusBarStyleBlackOpaque

第三步:在每个viewWillAppear中添加这个来调整iOS7的状态栏高度

    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7) {
        CGRect frame = [UIScreen mainScreen].bounds;
        frame.origin.y+=20.0;
        frame.size.height-= 20.0;
        self.view.frame = frame;
        [self.view layoutIfNeeded];
    }

这可能太晚了,但我有一些东西可以贡献,这可能会帮助到某人,我试图子类化UINavigationBar,并想让它看起来像ios 6黑色状态栏和白色状态栏文本。

以下是我发现的方法

        self.navigationController?.navigationBar.clipsToBounds = true
        self.navigationController?.navigationBar.translucent = false
        self.navigationController?.navigationBar.barStyle = .Black
        self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()

它使我的状态栏背景为黑色,状态栏文本为白色,导航栏为白色。

iOS 9.3, XCode 7.3.1

更新(新解决方案) 本次更新是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只有在详细信息窗格的“文件检查器”(最左边的图标)中取消视图控制器的“使用自动布局”时才会出现增量。