在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以下的应用程序窗口边界,在App委托didFinishLaunchingWithOptions方法中:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
if ([HMService getIOSVersion] < 7) {
    // handling statusBar (iOS6) by leaving top 20px for statusbar.
    screenBounds.origin.y = 20;
    self.window = [[UIWindow alloc] initWithFrame:screenBounds];
}
else {
    self.window = [[UIWindow alloc] initWithFrame:screenBounds];
}

其他回答

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

在你的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

这可能是一个压倒性的问题,如果你使用自动布局,因为你不能直接操作帧。有一个简单的解决方案,不需要太多的工作。

我最终在一个实用工具类中编写了一个实用方法,并从所有视图控制器的viewDidLayoutSubviews方法中调用它。

+ (void)addStatusBarIfiOS7:(UIViewController *)vc
    {
        if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
            CGRect viewFrame = vc.view.frame;
            if(viewFrame.origin.y == 20) {
                //If the view's y origin is already 20 then don't move it down.
                return;
            }
            viewFrame.origin.y+=20.0;
            viewFrame.size.height-= 20.0;
            vc.view.frame = viewFrame;
            [vc.view layoutIfNeeded];
        }
    }

重写视图控制器中的viewDidLayoutSubviews方法,在那里你想要状态栏。它会让你通过自动布局的负担。

- (void)viewDidLayoutSubviews
{
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    [super viewDidLayoutSubviews];
    [MyUtilityClass addStatusBarIfiOS7:self];
}

最简单的方法就是在最新的Xcode上安装一个旧的SDK。

如何安装旧SDK到最新的Xcode?

你可以从http://www.4shared.com/zip/NlPgsxz6/iPhoneOS61sdk.html获取iOS 6.1 SDK,也可以下载旧的Xcode并从其内容中获取SDK 解压并粘贴到/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/ sdk 重新启动xcode。 你现在可以在项目的构建设置中选择一个旧的SDK

希望对你有所帮助。这对我有用=)

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

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的状态栏!~ " ~

这是我写的一篇博客文章的交叉发布,但这里是关于iOS 7上的状态栏、导航栏和容器视图控制器的完整概述:

There is no way to preserve the iOS 6 style status bar layout. The status bar will always overlap your application on iOS 7 Do not confuse status bar appearance with status bar layout. The appearance (light or default) does not affect how the status bar is laid out (frame/height/overlap). It is important to note as well that the system status bar no longer has any background color. When the API refers to UIStatusBarStyleLightContent, they mean white text on a clear background. UIStatusBarStyleDefault is black text on a clear background. Status bar appearance is controlled along one of two mutually-exclusive basis paths: you can either set them programmatically in the traditional manner, or UIKit will update the appearance for you based on some new properties of UIViewController. The latter option is on by default. Check your app’s plist value for “ViewController-Based Status Bar Appearance” to see which one you’re using. If you set this value to YES, every top-level view controller in your app (other than a standard UIKit container view controller) needs to override preferredStatusBarStyle, returning either the default or the light style. If you edit the plist value to NO, then you can manage the status bar appearance using the familiar UIApplication methods. UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points, depending on a rather strange and undocumented set of constraints. If the UINavigationController detects that the top of its view’s frame is visually contiguous with its UIWindow’s top, then it draws its navigation bar with a height of 64 points. If its view’s top is not contiguous with the UIWindow’s top (even if off by only one point), then it draws its navigation bar in the “traditional” way with a height of 44 points. This logic is performed by UINavigationController even if it is several children down inside the view controller hierarchy of your application. There is no way to prevent this behavior. If you supply a custom navigation bar background image that is only 44 points (88 pixels) tall, and the UINavigationController’s view’s bounds matches the UIWindow’s bounds (as discussed in #4), the UINavigationController will draw your image in the frame (0,20,320,44), leaving 20 points of opaque black space above your custom image. This may confuse you into thinking you are a clever developer who bypassed rule #1, but you are mistaken. The navigation bar is still 64 points tall. Embedding a UINavigationController in a slide-to-reveal style view hierarchy makes this abundantly clear. Beware of the confusingly-named edgesForExtendedLayout property of UIViewController. Adjusting edgesForExtendedLayout does nothing in most cases. The only way UIKit uses this property is if you add a view controller to a UINavigationController, then the UINavigationController uses edgesForExtendedLayout to determine whether or not its child view controller should be visible underneath the navigation bar / status bar area. Setting edgesForExtendedLayout on the UINavigationController itself does nothing to alter whether or not the UINavigationController has a 44 or 64 point high navigation bar area. See #4 for that logic. Similar layout logic applies to the bottom of your view when using a toolbar or UITabBarController. If all you are trying to do is prevent your custom child view controller from underlapping the navigation bar when inside a UINavigationController, then set edgesForExtendedLayout to UIRectEdgeNone (or at least a mask that excludes UIRectEdgeTop). Set this value as early as possible in the life cycle of your view controller. UINavigationController and UITabBarController will also try to pad the contentInsets of table views and collection views in its subview hierarchy. It does this in a manner similar to the status bar logic from #4. There is a programmatic way of preventing this, by setting automaticallyAdjustsScrollViewInsets to NO for your table views and collection views (it defaults to YES). This posed some serious problems for Whisper and Riposte, since we use contentInset adjustments to control the layout of table views in response to toolbar and keyboard movements. To reiterate: there is no way to return to iOS 6 style status bar layout logic. In order to approximate this, you have to move all the view controllers of your app into a container view that is offset by 20 points from the top of the screen, leaving an intentionally black view behind the status bar to simulate the old appearance. This is the method we ended up using in Riposte and Whisper. Apple is pushing very hard to ensure that you don’t try to do #9. They want us to redesign all our apps to underlap the status bar. There are many cogent arguments, however, for both user experience and technical reasons, why this is not always a good idea. You should do what is best for your users and not simply follow the whimsy of the platform.