58

我正在将我的应用程序迁移到 iOS 7。为了处理状态栏问题,我添加了此代码

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
    CGRect frame = self.navigationController.view.frame;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        frame.origin.y = 20;
    }
    else
    {
        frame.origin.x = 20;
    }
    [self.navigationController.view setFrame:frame];
}

这在正常情况下工作正常。如果我正在更改方向(应用程序仅支持横向方向)或呈现任何视图控制器并关闭模型视图控制器,我的视图控制器对齐方式已更改。状态栏再次与我的视图控制器重叠。这段代码根本不起作用。请指导我解决此状态栏问题。

案例 2:这就是我展示视图控制器的方式

ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    reader.supportedOrientationsMask = ZBarOrientationMaskLandscape;
else
    reader.supportedOrientationsMask = ZBarOrientationMaskPortrait;

    [self presentModalViewController:reader animated:YES];

参考:

在此处输入图像描述

提前致谢。

4

11 回答 11

133

修复 IOS 7 中的状态栏问题

最后,我使用 xcode5 中的 delta value 属性修复了状态栏重叠问题。首先,我将原点 - y 20pxl 增加到 Xib 中使用的所有控制器(它似乎只在 IOS 7 中工作正常),之后我将所有视图控制器原点 -y 的增量值设置为 -20 它工作正常在 iOS 6 和 iOS 7 中。

执行此操作的步骤。

Xcode 5 提供预览选项以根据操作系统版本在不同视图中查看 xib 的外观。

从助理编辑器中选择预览选项

点击助理编辑

在此处输入图像描述

并选择预览选项以预览不同版本的选定视图控制器。

在此处输入图像描述

视图控制器视图预览选项。

在此处输入图像描述

在预览中,您可以找到切换选项以预览不同版本的视图。在预览中,如果状态栏问题未通过切换版本正确修复,您可以清楚地感受到状态栏问题。

解决状态栏问题的三个步骤: 第 1 步: 确保视图在 File inspector 中以我们 7.0 及更高版本为目标

在此处输入图像描述

第 2 步:将视图控制器中添加的所有控件的原点 - y 增加 20 像素(正好是状态栏的大小)。

第 3 步:将所有控件的原点 y 的增量值设置为 -20,然后只有它会根据版本自动调整。立即使用预览,感受控件因增量值而自动调整的不同。

在此处输入图像描述

修复状态栏问题后,显示模型视图(ZbarSDk 控制器)时的问题也会自动修复。

预览画面:

在此处输入图像描述

于 2013-09-26T10:19:33.637 回答
15

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

首先-> 转到您的info.plist File添加状态栏样式-> 透明黑色样式(Alpha 为 0.5)

现在,它开始了:-

将此代码添加到您的 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;
   }
于 2013-11-16T12:39:24.363 回答
6
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

        [application setStatusBarStyle:UIStatusBarStyleLightContent];
         [application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

        self.window.clipsToBounds =YES;            
        self.window.frame =CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
    }


   [self.window makeKeyAndVisible];
    return YES;
}

将以下内容设置为 info.plist

查看基于控制器的状态栏外观 = NO;

于 2014-01-03T06:45:36.583 回答
3

简单的答案:

将视图的顶部与“顶部布局指南”对齐,但控制拖动“顶部布局指南”到您的视图并设置“垂直”约束。请参阅此答案以获取图片参考。

它的工作方式是 - “顶部布局指南”会自动调整状态栏何时存在或不存在,并且一切正常 - 无需编码!

PS在这个特定的例子中,在底部显示的背景也应该通过设置视图底部的适当垂直约束来解决,它的超级视图,或者其他什么......

于 2014-11-26T15:58:13.183 回答
3

要在 ios7 中隐藏状态栏,请执行以下简单步骤:

在 Xcode 中转到“ Resources”文件夹并打开“ (app name)-Info.plist file”。

  • 检查“ View controller based status bar appearance”键并设置其值“ NO
  • 检查“ Status bar is initially hidden”键并设置其值“ YES

如果键不存在,那么您可以通过选择information property list顶部的“”并单击+图标来添加它

于 2014-01-15T13:26:14.620 回答
3

听说我们可以一次为所有视图执行此操作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
// Notification for the orientaiton change
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationDidChangeStatusBarOrientation:)
                                             name:UIApplicationDidChangeStatusBarOrientationNotification
                                           object:nil];

// Window framing changes condition for iOS7 or greater
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    statusBarBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.window.frame.size.width, 20)];//statusBarBackgroundView is normal uiview
    statusBarBackgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.730];
    [self.window addSubview:statusBarBackgroundView];
    self.window.bounds = CGRectMake(0, -20, self.window.frame.size.width, self.window.frame.size.height);
}
// Window framing changes condition for iOS7 or greater

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];


return YES;
}

当我们使用方向时,我们可以在应用程序委托中添加以下方法以通过方向设置它。

- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    statusBarBackgroundView.hidden = YES;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    int width = [[UIScreen mainScreen] bounds].size.width;
    int height = [[UIScreen mainScreen] bounds].size.height;

    switch (orientation) {
        case UIInterfaceOrientationLandscapeLeft:
            self.window.bounds =  CGRectMake(-20,0,width,height);
            statusBarBackgroundView.frame = CGRectMake(-20, 0, 20, height);
            break;
        case UIInterfaceOrientationLandscapeRight:
            self.window.bounds =  CGRectMake(20,0,width,height);
            statusBarBackgroundView.frame = CGRectMake(320, 0, 20, height);
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            statusBarBackgroundView.frame = CGRectMake(0, 568, width, 20);
            self.window.bounds =  CGRectMake(0, 20, width, height);
            break;
        default:
            statusBarBackgroundView.frame = CGRectMake(0, -20, width, 20);
            self.window.bounds =  CGRectMake(0, -20, width, height);
            break;
    }
    statusBarBackgroundView.hidden = NO;
}
}

您应该为其添加以下导航控制器类别

。H

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface UINavigationController (iOS6fix)

@end

.m

#import "UINavigationController+iOS6fix.h"
@implementation UINavigationController (iOS6fix)  

-(BOOL)shouldAutorotate
{
      return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
      return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
于 2013-11-30T12:09:33.940 回答
1
#define _kisiOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

 if (_kisiOS7)
    {
        [[UINavigationBar appearance] setBarTintColor:_kColorFromHEX(@"#011C47")];
    }
    else
    {
        [[UINavigationBar appearance] setBackgroundColor:_kColorFromHEX(@"#011C47")];
        [[UINavigationBar appearance] setTintColor:_kColorFromHEX(@"#011C47")];
    }
于 2014-05-19T14:44:56.177 回答
1

使用 Salesforce SDK 2.1 (Cordova 2.3.0),我们必须执行以下操作才能让状态栏出现在应用程序的初始加载并从后台返回(iPhone 和 iPad):

与此处发布的其他解决方案相反,这个解决方案似乎可以在设备旋转后幸存下来。

1-创建一个类别SFHybridViewController

#import "SFHybridViewController+Amalto.h"

@implementation SFHybridViewController (Amalto)

- (void)viewWillAppear:(BOOL)animated
    {
        //Lower screen 20px on ios 7
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
            CGRect viewBounds = self.view.bounds;
            viewBounds.origin.y = 20;
            viewBounds.size.height = viewBounds.size.height - 20;
            self.webView.frame = viewBounds;
        }

    [super viewWillAppear:animated];
    }

    - (void)viewDidLoad
    {

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
            CGRect viewBounds = self.view.bounds;
            viewBounds.origin.y = 20;
            viewBounds.size.height = viewBounds.size.height - 20;
            self.webView.frame = viewBounds;
        }
        [super viewDidLoad];
    }

@end

2-添加到AppDelegate.m进口

#import "SFHybridViewController+Amalto.h"

3-在方法结束时didFinishLaunchingWithOptions注入AppDelegate

//Make the status bar appear
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    [application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}

4-添加到App-Info.plist属性

View controller-based status bar appearance有价值NO

于 2014-01-29T17:23:54.403 回答
1

有几种不同的方法。一种方法是使用 .plist 文件

  • 添加一个新键“查看基于控制器的状态栏外观”并将值设置为“”。
  • 添加另一个键“状态栏最初隐藏”并将值设置为“”。

这将隐藏整个项目的状态栏。

方法截图

于 2014-08-06T06:12:30.230 回答
1

我通过使用下面的代码解决了这个问题

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

       if ([UIDevice currentDevice].systemVersion.floatValue>=7) {
        CGRect frame = self.window.frame;
       frame.size.width -= 20.0f;
      frame.origin.x+= 20.0f;
       self.window.frame = frame;
    }


 if(portrait)

    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
    [application setStatusBarStyle:UIStatusBarStyleLightContent];

    CGRect frame = self.window.frame;
    frame.origin.y += 20.0f;
    frame.size.height -= 20.0f;
    self.window.frame = frame;
}
return YES;

 }
于 2014-04-15T07:40:34.023 回答
1

只需在viewWillAppear.

 if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
 }
于 2015-06-22T11:47:19.310 回答