iOS 7状态栏在iPhone应用程序中恢复到iOS 6默认样式?

问题描述 投票:291回答:25

在iOS 7中,UIStatusBar的设计方式与它的视图合并如下:

(由Tina Tavčar设计的GUI)

  • 这很酷,但是当你在视图的顶部有一些东西时它会使你的视图陷入混乱,并且它会与状态栏重叠。
  • 是否有一个简单的解决方案(例如在info.plist中设置属性)可以改变它的工作方式[不重叠]回到它在iOS6中的状态?
  • 我知道一个更直接的解决方案是为每个视图控制器提供self.view.center.x + 20点,但更改它们会使其他尺寸变得更大(具有不同的self.view.center.x会导致自定义segue等问题)并突然变成繁琐的工作最好避免。
  • 如果有人能为我提供单线解决方案,我真的很高兴。

附:我知道我可以通过做某事来隐藏状态栏

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

didFinishLaunchingWithOptions方法,但这是一个解决方法,一个避免问题的捷径,所以我不认为这是一个真正的解决方案。

statusbar ios7
25个回答
449
投票

这是来自a blog post I wrote的交叉发布,但这是iOS 7上状态栏,导航栏和容器视图控制器的完整纲要:

  1. 无法保留iOS 6样式状态栏布局。状态栏将始终与iOS 7上的应用程序重叠
  2. 不要将状态栏外观与状态栏布局混淆。外观(亮或默认)不会影响状态栏的布局方式(帧/高度/重叠)。重要的是要注意系统状态栏不再具有任何背景颜色。当API引用UIStatusBarStyleLightContent时,它们表示清晰背景上的白色文本。 UIStatusBarStyleDefault是清晰背景上的黑色文本。
  3. 状态栏外观是根据两个互斥的基本路径之一控制的:您可以以传统方式以编程方式设置它们,或者UIKit将根据UIViewController的一些新属性为您更新外观。后一个选项默认启用。检查应用程序的“基于ViewController的状态栏外观”的plist值,看看你正在使用哪一个。如果将此值设置为YES,则应用程序中的每个顶级视图控制器(标准UIKit容器视图控制器除外)都需要覆盖preferredStatusBarStyle,返回默认样式或浅色样式。如果将plist值编辑为NO,则可以使用熟悉的UIApplication方法管理状态栏外观。
  4. UINavigationController会将其UINavigationBar的高度更改为44点或64点,具体取决于一组相当奇怪且未记录的约束。如果UINavigationController检测到其视图框架的顶部与UIWindow的顶部在视觉上是连续的,那么它将绘制高度为64点的导航栏。如果它的视图顶部与UIWindow的顶部不连续(即使只关闭一个点),那么它以“传统”方式绘制其导航栏,高度为44点。即使在应用程序的视图控制器层次结构中有多个子项,UINavigationController也会执行此逻辑。没有办法阻止这种行为。
  5. 如果您提供的高度仅为44磅(88像素)的自定义导航栏背景图像,并且UINavigationController的视图边界与UIWindow的边界匹配(如#4中所述),则UINavigationController将在框架中绘制您的图像(0,20,320) ,44),在自定义图像上方留下20个不透明的黑色空间。这可能会让你误以为你是一个聪明的开发者绕过了规则#1,但你错了。导航栏仍然是64点高。在幻灯片到显示样式视图层次结构中嵌入UINavigationController使得这一点非常清晰。
  6. 注意UIViewController的容易混淆的edgesForExtendedLayout属性。调整edgesForExtendedLayout在大多数情况下不执行任何操作。 UIKit使用此属性的唯一方法是,如果将视图控制器添加到UINavigationController,则UINavigationController使用edgesForExtendedLayout来确定其子视图控制器是否应在导航栏/状态栏区域下可见。在UINavigationController上设置edgesForExtendedLayout本身不会改变UINavigationController是否具有44或64点高导航栏区域。有关该逻辑,请参阅#4。使用工具栏或UITabBarController时,类似的布局逻辑适用于视图的底部。
  7. 如果你想要做的就是阻止你的自定义子视图控制器在UINavigationController内部使用导航栏,那么将edgesForExtendedLayout设置为UIRectEdgeNone(或者至少是一个排除UIRectEdgeTop的掩码)。在视图控制器的生命周期中尽早设置此值。
  8. UINavigationController和UITabBarController还将尝试在其子视图层次结构中填充表视图和集合视图的contentInsets。它以类似于#4的状态栏逻辑的方式执行此操作。通过为表视图和集合视图设置automaticAdjustsScrollViewInsets为NO(默认为YES),有一种编程方法可以防止这种情况。这给Whisper和Riposte带来了一些严重的问题,因为我们使用contentInset调整来控制表视图的布局以响应工具栏和键盘的移动。
  9. 重申:没有办法返回iOS 6样式状态栏布局逻辑。为了估算这一点,您必须将应用程序的所有视图控制器移动到距离屏幕顶部偏移20点的容器视图中,在状态栏后面留下有意黑色视图以模拟旧外观。这是我们最终在Riposte和Whisper中使用的方法。
  10. Apple非常努力地确保你不要尝试#9。他们希望我们重新设计我们的所有应用程序以覆盖状态栏。然而,对于用户体验和技术原因,有许多有说服力的论据,为什么这并不总是一个好主意。您应该为用户做最好的事情而不是简单地遵循平台的奇思妙想。

5
投票

我在所有的视图控制器中使用它,这很简单。在所有viewDidLoad方法中添加以下行:

- (void)viewDidLoad{
    //add this 2 lines:
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

    [super viewDidLoad];
}

4
投票

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

第1步:改变单个viewController

[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];

第2步:改变整个应用程序

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

第3步:在每个viewWillAppear中添加此项以调整statusbariOS7高度

    [[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];
    }

3
投票

Interface Builder中有一个选项可调用iOS 6/7 Delta属性,旨在解决偏移问题。

在Stack Overflow问题Interface Builder: What are the UIView's Layout iOS 6/7 Deltas for?中查看它。


3
投票

我已经在iOS 7中获得了像iOS 6这样的状态栏。

在info.plist中将UIViewControllerBasedStatusBarAppearance设置为NO

- (BOOL)application:(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方法之类的窗口分配后设置windowHeight Userdefaults值

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

2
投票

如果您正在使用“界面”构建器,请尝试以下操作

在你的xib文件中:

1)选择主视图,将背景颜色设置为黑色(或者您希望状态栏显示的颜色)

2)确保背景是一个自包含的子视图,定位为控制器视图的顶级子视图。 移动背景以成为控制器视图的直接子项。检查自动调整面板以确保已锁定所有框架边缘,激活两个灵活性轴,如果这是UIImageView,请将内容模式设置为“缩放以填充”。以编程方式,这将转换为设置为UIViewContentModeScaleToFill的contentMode,并将其自动调整大小掩码设置为(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)。

3)现在将锁定到自上而下的所有内容移动20分,并将iOS 6/7 delta Y设置为-20。 锁定到自动调整面板中顶部框架的所有顶级子项需要向下移动20pts并将其iOS 6/7 delta Y设置为-20。 (Cmd选择所有这些,然后点击箭头20次 - 有没有更好的方式?)

4)调整具有灵活高度的所有上述项目的iOS 6/7增量高度。锁定到框架顶部和底部并且在自动调整面板中启用了灵活高度的任何项目也必须将其iOS 6/7增量高度设置为20.这包括上面提到的背景视图。这可能看起来反直觉,但由于应用这些的顺序,这是必要的。首先设置帧高(基于设备),然后应用增量,最后根据所有子帧的偏移位置应用自动调整掩码 - 仔细考虑一下,这是有意义的。

5)最后,锁定到底部框架而不是顶部框架的物品根本不需要任何增量。

这将为您提供iOS7和iOS6中相同的状态栏。

另一方面,如果您希望iOS7样式同时保持iOS6兼容性,则将背景视图的增量Y / delta高度值设置为0。

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


2
投票

我的解决方案是在iOS 7上在窗口顶部添加一个高度为20点的UIView。然后我在AppDelegate类中创建了一个方法来显示/隐藏“实体”状态栏背景。在application:didFinishLaunchingWithOptions:

// ...

// Add a status bar background
self.statusBarBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 20.0f)];
self.statusBarBackground.backgroundColor = [UIColor blackColor];
self.statusBarBackground.alpha = 0.0;
self.statusBarBackground.userInteractionEnabled = NO;
self.statusBarBackground.layer.zPosition = 999; // Position its layer over all other views
[self.window addSubview:self.statusBarBackground];

// ...
return YES;

然后我创建了一个淡入/淡出黑色状态栏背景的方法:

- (void) showSolidStatusBar:(BOOL) solidStatusBar
{
    [UIView animateWithDuration:0.3f animations:^{
        if(solidStatusBar)
        {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            self.statusBarBackground.alpha = 1.0f;
        }
        else
        {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
            self.statusBarBackground.alpha = 0.0f;
        }
    }];
}

我现在需要做的就是在需要时打电话给[appDelegate showSolidStatusBar:YES]


2
投票

如果您使用自动布局,这可能是一个压倒性的问题,因为您无法再直接操作帧。没有太多工作就有一个简单的解决方案。

我最终在Utility Class中编写了一个实用程序方法,并从所有视图控制器的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方法,您需要状态栏。它将帮助您解决Autolayout的负担。

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

1
投票

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

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

  1. 你可以从http://www.4shared.com/zip/NlPgsxz6/iPhoneOS61sdk.html获得iOS 6.1 SDK或下载旧的Xcode并从其内容中获取SDK
  2. 将此文件夹解压缩并粘贴到/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs
  3. 重启xcode。
  4. 您现在可以在项目的构建设置中选择较旧的SDK

希望它能帮到你。它适用于我=)


1
投票

使用presentViewController:animated:completion:搞砸了window.rootViewController.view,我必须找到一个不同的方法来解决这个问题。我终于通过继承我的rootViewController的UIView来使用模态和旋转。

。H

@interface RootView : UIView

@end

.M

@implementation RootView

-(void)setFrame:(CGRect)frame
{
    if (self.superview && self.superview != self.window)
    {
        frame = self.superview.bounds;
        frame.origin.y += 20.f;
        frame.size.height -= 20.f;
    }
    else
    {
        frame = [UIScreen mainScreen].applicationFrame;
    }

    [super setFrame:frame];
}

- (void)layoutSubviews
{
    self.frame = self.frame;

    [super layoutSubviews];
}

@end

您现在有一个强大的iOS7动画解决方法。


1
投票

我这个答案迟到了,但我只想分享我的所作所为,这基本上是最简单的解决方案

首先 - >转到你的info.plist文件并添加状态栏样式 - >透明黑色样式(0.5的Alpha)

现在,它来了: -

在AppDelegate.m中添加此代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
     //Whatever your code goes here
  if(kDeviceiPad){

     //adding status bar for IOS7 ipad
         if (IS_IOS7) {
              UIView *addStatusBar = [[UIView alloc] init];
              addStatusBar.frame = CGRectMake(0, 0, 1024, 20);
              addStatusBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; //change this to match your navigation bar
              [self.window.rootViewController.view addSubview:addStatusBar];
                    }
                }
    else{

         //adding status bar for IOS7 iphone
        if (IS_IOS7) {
            UIView *addStatusBar = [[UIView alloc] init];
            addStatusBar.frame = CGRectMake(0, 0, 320, 20);
            addStatusBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; //You can give your own color pattern
            [self.window.rootViewController.view addSubview:addStatusBar];
        }

    return YES;
   }

122
投票

2013年9月19日更新:

通过添加self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);来修复扩展错误

更正了NSNotificationCenter声明中的拼写错误

2013年9月12日更新:

纠正UIViewControllerBasedStatusBarAppearanceNO

为屏幕旋转的应用添加了解决方案

添加了一种方法来更改状态栏的背景颜色。

显然,没有办法将iOS7状态栏恢复为在iOS6中的工作方式。

但是,我们总是可以编写一些代码并将状态栏转换为iOS6,这是我能想到的最短路径:

  1. UIViewControllerBasedStatusBarAppearance中将NO设置为info.plist(要选择不让视图控制器调整状态栏样式,以便我们可以使用UIApplicationstatusBarStyle方法设置状态栏样式。)
  2. 在AppDelegate的application:didFinishLaunchingWithOptions中,请致电 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;

为了:

  1. 检查它是否是iOS 7。
  2. 将状态栏的内容设置为白色,而不是UIStatusBarStyleDefault。
  3. 避免其框架超出可见边界的子视图显示(对于从顶部向主视图设置动画的视图)。
  4. 通过移动和调整应用程序的窗口框架,创建状态栏占用空间的错觉,就像在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状态栏错觉。

要更改状态栏的背景颜色:

 @property (retain, nonatomic) UIWindow *background;

AppDelegate.h,让background成为你班上的财产,防止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


0
投票

我非常简单的解决方案(假设您只支持垂直方向)是在App委托didFinishLaunchingWithOptions方法中重新定义低于7的iOS版本的应用程序窗口边界:

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];
}

0
投票

您可以一起隐藏状态栏。所以你的应用程序将全屏显示。我认为这是你得到的最好的。

UIStatusBarStyleNone或在目标设置中设置。


0
投票

步骤隐藏iOS 7中的状态栏:

1.转到您的应用程序info.plist文件。

2.And Set,View基于控制器的状态栏外观:布尔值NO

希望我解决状态栏问题.....


0
投票

为了继续使用setStatusBarHidden:我使用这个类别:

@interface UIApplication (StatusBar)

-(void)setIOS7StatusBarHidden:(BOOL)statusBarHidden;

@end

@implementation UIApplication (StatusBar)

-(void)setIOS7StatusBarHidden:(BOOL)statusBarHidden{
    if (!IOS7) {
        [self setStatusBarHidden:statusBarHidden];
        return;
     }

    if ([self isStatusBarHidden] == statusBarHidden) {
        return;
    }

    [self setStatusBarHidden:statusBarHidden];
    [self keyWindow].clipsToBounds = YES;
    CGFloat offset = statusBarHidden ? 0 : 20;
    [self keyWindow].frame =  CGRectMake(0,offset,[self keyWindow].frame.size.width,[self keyWindow].frame.size.height-offset);
    [self keyWindow].bounds = CGRectMake(0, offset, [self keyWindow].frame.size.width,[self keyWindow].frame.size.height);
}

@end

0
投票

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

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

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


0
投票

这可能为时已晚,无法分享,但我有一些可以帮助某人的贡献,我试图对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


41
投票

更新(新解决方案)

此更新是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];
}

对于Interface Builder,这适用于使用iOS 6打开时;它从0像素开始。

Note: iOS 6/7 Deltas only appear if you uncheck "Use Autolayout" for the View Controller in the "File Inspector" (left-most icon) in the details pane.


26
投票

解决方案

通过重写方法在viewcontroller或rootviewcontroller中设置它:

-(BOOL) prefersStatusBarHidden
    {
        return YES;
    }

17
投票

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

目标:

这种方法的目标是在iOS 7中重建与iOS 6中相同的状态栏样式(请参阅问题标题“iOS 7 Status Bar Back to iOS 6 style?”)。

摘要:

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

假设:

为了在按照以下步骤获得所需结果时,假设View controller-based status bar appearance设置为NO并且您的Status bar style设置为“透明黑色样式(alpha of 0.5)”或“不透明黑色样式”。可以在项目设置中的“信息”下找到/或添加这两个设置。

脚步:

  • 将子视图添加到UIWindow以充当状态栏背景。为此,请在application: didFinishLaunchingWithOptions:之后将以下内容添加到AppDelegate的makeKeyAndVisibleif (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]; }
  • 由于您仅以编程方式为iOS 7添加了背景,因此您必须相应地调整与状态栏重叠的UI元素的布局,同时保留其iOS6的布局。要实现此目的,请执行以下操作: 确保为您的Storyboard取消选中Use Autolayout(这是因为“尺寸检查器”中未显示“iOS 6/7 Deltas”)。去做这个: 选择您的Storyboard文件 显示实用程序 选择“显示文件检查器” 在“Interface Builder Document”下,取消选中“Use Autolayout” 或者,为了帮助您在应用iOS 7和6时监视iOS 7和6的布局更改,请选择“助理编辑器”,选择“预览”和“iOS 6.1或更早版本”: 现在选择要调整的UI元素,使其不再与状态栏重叠 在Utilities列中选择“Show the Size Inspector” 沿Y轴重新定位UI元素的量与状态栏bg高度相同: 并将Y的iOS6 / 7 Deltas值更改为与状态栏bg高度相同的负值(如果您正在使用它,请注意iOS 6预览中的更改):

备择方案:

要在故事板大量项目中添加更少的代码并使状态栏背景自动旋转,而不是以编程方式为状态栏添加背景,您可以为位于所述viewcontroller主视图顶部的每个视图控制器添加彩色视图。然后,您可以将此新视图的高度增量更改为与视图高度相同的负数(以使其在iOS 6下消失)。

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


12
投票

如果您不希望视图控制器与状态栏(和导航栏)重叠,请取消选中Interface BuilderXcode中的“在顶部栏下延伸边缘”框。


11
投票

Apple发布了Technical Q&A QA1797: Preventing the Status Bar from Covering Your Views。它适用于iOS 6和iOS 7版本。


8
投票

我已经查看了许多很多很多教程来解决这个问题。但它们都不起作用!这是我的解决方案,它适用于我:

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个像素的self.view上。就这样。然后,屏幕截图将像iOS 6一样显示。我讨厌iOS7状态栏! 〜“〜


6
投票

Archy Holt答案的一个小替代品,更简单一点:

一个。在info.plist中将UIViewControllerBasedStatusBarAppearance设置为NO

湾在AppDelegateapplication:didFinishLaunchingWithOptions:,请致电:

if ([[UIDevice currentDevice].systemVersion floatValue] < 7)
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
else
{
    // handling statusBar (iOS7)
    application.statusBarStyle = UIStatusBarStyleLightContent;
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.window.clipsToBounds = YES;

    // handling screen rotations for statusBar (iOS7)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidChangeStatusBarOrientationNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}

并添加方法:

- (void)applicationDidChangeStatusBarOrientationNotification:(NSNotification *)notification
{
    // handling statusBar (iOS7)
    self.window.frame = [UIScreen mainScreen].applicationFrame;
}

您还可以考虑将UIWindow子类化以处理UIApplicationDidChangeStatusBarOrientationNotification本身。

© www.soinside.com 2019 - 2024. All rights reserved.