Status bar and navigation bar issue in IOS7

2018-12-31 10:12发布

I am migrating my application to iOS 7. For handing the status bar issue I have added this code

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

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:

enter image description here

Thanks in advance.

11条回答
泪湿衣
2楼-- · 2018-12-31 10:32
- (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

View controller-based status bar appearance = NO;

查看更多
君临天下
3楼-- · 2018-12-31 10:34

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!

P.S. 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...

查看更多
临风纵饮
4楼-- · 2018-12-31 10:34

There are several different ways. One approach is to use .plist file

  • Add a new key "View controller-based status bar appearance" and set value as "NO".
  • Add another key "Status bar is initially hidden" and set value as "YES".

This will hide status bar throughout project.

Screenshot for approach

查看更多
宁负流年不负卿
5楼-- · 2018-12-31 10:36

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.

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

#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
查看更多
孤独总比滥情好
6楼-- · 2018-12-31 10:37

To hide status bar in ios7 follow these simple steps :

In Xcode goto "Resources" folder and open "(app name)-Info.plist file".

  • check for "View controller based status bar appearance" key and set its value "NO"
  • check for "Status bar is initially hidden" key and set its value "YES"

If the keys are not there then you can add it by selecting "information property list" at top and click + icon

查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 10:45

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)

Now ,here it Goes:-

Add this code in your 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;
   }
查看更多
登录 后发表回答