简体   繁体   English

IOS7中的状态栏和导航栏问题

[英]Status bar and navigation bar issue in IOS7

I am migrating my application to iOS 7. For handing the status bar issue I have added this code 我正在将我的应用程序迁移到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];
}

This is working fine in normal case. 这在正常情况下工作正常。 If I am changing orientation (app supports only landscape orientation) or presenting any view controller and dismissing model view controller my view controller alignment changed. 如果我正在更改方向(应用程序仅支持横向方向)或呈现任何视图控制器并关闭模型视图控制器,我的视图控制器对齐方式已更改。 The status bar again overlaps my view controller. 状态栏再次与我的视图控制器重叠。 This piece of code is not working at all. 这段代码根本不起作用。 Please guide me to fix this status bar issue. 请指导我修复此状态栏问题。

Case 2: This is how I am presenting my view controller 案例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];

Ref: 参考:

在此输入图像描述

Thanks in advance. 提前致谢。

Fix for status bar issue in IOS 7 修复了IOS 7中的状态栏问题

Finally i fixed the status bar over lap issue using the delta value property in xcode5. 最后,我使用xcode5中的delta值属性修复了状态栏。 First i have increased origin - y 20pxl to all the controller used in the Xib (it seams to be working fine only in IOS 7), after that i set the delta value for all the view controller origin -y to -20 it works fine in both ios 6 and IOS 7 . 首先我增加了原点 - 对于Xib中使用的所有控制器都是20pxl (它的接缝仅在IOS 7中工作正常),之后我将所有视图控制器原点的delta值设置为-y它工作正常在ios 6和IOS 7中

Steps to do that. 这样做的步骤。

Xcode 5 provide preview option to view the appearance of the xib in different view based on the OS version. Xcode 5提供预览选项,可根据操作系统版本查看不同视图中xib的外观。

choose preview option from assistant editor 从助理编辑器中选择预览选项

click assistant editor 点击助理编辑器

在此输入图像描述

and choose preview option to preview selected view controller in different version. 并选择预览选项以预览不同版本的所选视图控制器。 在此输入图像描述

view controller view preview option. 查看控制器视图预览选项。

在此输入图像描述

in preview you can find the toggle option to preview view in different version. 在预览中,您可以找到切换选项以预览不同版本的视图。 In preview u can feel the status bar issue clearly if its not fixed properly by toggle the version. 在预览中,如果通过切换版本未正确修复状态栏,则可以清楚地感觉到状态栏问题。

Three steps to fix the status bar issue: step 1: Make sure the view target us 7.0 and later in File inspector . 修复状态栏问题的三个步骤: 步骤1:确保视图在文件检查器中以7.0及更高版本为目标 在此输入图像描述

Step 2 : Increase the origin - y with 20 pixel (exactly the size of the status bar) for all the controls added in the view controller. 步骤2:增加原点 - y为视图控制器中添加的所有控件的20像素 (与状态栏的大小完全相同)。

Step 3 : Set the delta value of origin y to -20 for all the controls then only it will adjust automatically based on the version. 步骤3:将所有控件的原点y的delta值设置为-20,然后仅根据版本自动调整。 Use preview now and feel the differ that the controls automatically adjust because of the delta value. 现在使用预览,并感受控件因delta值而自动调整的差异。 在此输入图像描述

Once the status bar issue fixed, issue while presenting the model view (ZbarSDk controller) is also fixed automatically. 状态栏问题解决后,在显示模型视图(ZbarSDk控制器)时发出的问题也会自动修复。

Preview screen : 预览屏幕:

在此输入图像描述

I am late for this Answer, but i just want to share what i did, which is basically 我这个答案迟到了,但我只想分享我所做的,基本上就是这样
the easiest solution 最简单的解决方案

First of all-> Go to your info.plist File and add Status Bar Style->Transparent Black Style(Alpha of 0.5) 首先 - >转到info.plist File添加状态栏样式 - >透明黑色样式(0.5的Alpha)

Now ,here it Goes:- 现在,它来了: -

Add this code in your AppDelegate.m 在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;
   }
- (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;
}

set the following to info.plist 将以下内容设置为info.plist

View controller-based status bar appearance = NO; 查看基于控制器的状态栏外观= NO;

Hear we can do this for all views at once 听说我们可以同时为所有观点做到这一点

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

And While we are using orientation we can add below method in app delegate to set it via orientation. 虽然我们使用方向,但我们可以在app delegate中添加以下方法来通过方向设置它。

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

You should Add below navigation controller category for it 您应该为它添加以下导航控制器类别

.h 。H

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

@end

.m .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

To hide status bar in ios7 follow these simple steps : 要在ios7中隐藏状态栏,请按照以下简单步骤操作:

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

  • check for " View controller based status bar appearance " key and set its value " NO " 检查“ View controller based status bar appearance ”键并将其值设置为“ NO
  • check for " Status bar is initially hidden " key and set its value " YES " 检查“ Status bar is initially hidden ”键并将其值设置为“ YES

If the keys are not there then you can add it by selecting " information property list " at top and click + icon 如果没有按键,则可以通过选择顶部的“ information property list ”添加它,然后单击+图标

MUCH MUCH MUCH simpler answer: 很多很多更简单的答案:

Align the top of your view to the "top layout guide", but control-dragging "Top Layout Guide" to your view and setting the "vertical" constraint. 将视图顶部与“顶部布局指南”对齐,但控制 - 将“顶部布局指南”拖动到视图并设置“垂直”约束。 See this answer for a picture reference. 有关图片参考, 请参阅此答案

The way it works is - the "Top Layout Guide" will automagically ajust itself for when the status bar is or is not there, and it will all work - no coding required! 它的工作方式是 - “顶部布局指南”将自动调整状态栏,当状态栏存在或不存在时,它将全部工作 - 无需编码!

PS In this particular example, the background showing through at the bottom should also be resolved by setting an appropriate vertical constraint of the view's bottom, to it's superview, or whatever... PS在这个特定的例子中,底部显示的背景也应该通过设置视图底部的适当垂直约束,它的超视图或其他来解决...

i solved this by using below code 我用下面的代码解决了这个问题

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

 }
#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")];
    }

just set the following code in viewWillAppear . 只需在viewWillAppear设置以下代码即可。

 if ([[[UIDevice currentDevice] systemVersion] floatValue]<= 7) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
 }

With Salesforce SDK 2.1 (Cordova 2.3.0) we had to do the following to get the status bar appear on the initial load of the App and coming back from the background (iPhone and iPad): 使用Salesforce SDK 2.1(Cordova 2.3.0),我们必须执行以下操作才能在应用程序的初始加载状态下显示状态栏并从后台(iPhone和iPad)返回:

Contrarily to other solutions posted here, this one seems to survive rotation of the device. 与此处发布的其他解决方案相反,这个解决方案似乎在设备的旋转中存活。

1- Create a category of the SFHybridViewController 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- Add to AppDelegate.m imports 2- 添加到AppDelegate.m导入

#import "SFHybridViewController+Amalto.h"

3- Inject at the end of of method didFinishLaunchingWithOptions of AppDelegate 3- AppDelegate的方法didFinishLaunchingWithOptions的末尾注入

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

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

4- Add to App-Info.plist the property 4- 添加到App-Info.plist属性

View controller-based status bar appearance with value NO View controller-based status bar appearance ,其值为NO

There are several different ways. 有几种不同的方式。 One approach is to use .plist file 一种方法是使用.plist文件

  • Add a new key " View controller-based status bar appearance " and set value as " NO ". 添加新密钥“ 查看基于控制器的状态栏外观 ”并将值设置为“ NO ”。
  • Add another key " Status bar is initially hidden " and set value as " YES ". 添加另一个键“ 状态栏最初隐藏 ”并将值设置为“ ”。

This will hide status bar throughout project. 这将隐藏整个项目的状态栏。

方法的截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM