0

I have created an application using XIB without Status Bar before for iOS7, now i need to add Status bar on my app and Status bar background color should same as Navigation bar background color. So i have tried like (In my info.plist) :

1) Set View controller-based status bar appearance to NO
2) Set Status bar style to UIStatusBarStyleLightContent 

here is my code for App Delegate:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:(51/255.0) green:(51/255.0) blue:(51/255.0) alpha:1.0]];

So i am getting the output like the below image :

enter image description here

also i am getting misplacement of UIButtons that i have given below in my screen (It's hiding 20 pixels).

Can you please help me that how can i fix this issue? I need my output like the below image :

enter image description here

Any help will be very appreciated, Thanks.

Third screen :

enter image description here

user2786
  • 656
  • 4
  • 13
  • 35

3 Answers3

1

Add following code in your viewDidLoad method :

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

if (systemVersion >= 7.0) 
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
Naga Mallesh Maddali
  • 1,005
  • 10
  • 19
  • Mallesh MaddaliI have added the same in my viewDidLoad method, but still i am getting the same output as image 1. How to separate Status bar and Navigation bar? – user2786 Jun 30 '14 at 07:23
  • Try by removing following code from AppDelegate: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; – Naga Mallesh Maddali Jun 30 '14 at 07:25
  • Thanks @Naga I have removed mentioned lines.. now i am getting output without status bar. – user2786 Jun 30 '14 at 07:29
  • Check by adding only this line : [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; – Naga Mallesh Maddali Jun 30 '14 at 07:31
  • After adding this.. i am getting output with Status bar as my attached image 3. pls check.. – user2786 Jun 30 '14 at 07:38
  • @user2786, try to use UIBarPositioningDelegate solution from this post : http://stackoverflow.com/questions/18948505/uinavigationbar-status-bar-issue-in-ios7. I think your problem is because of auto layout. – Naga Mallesh Maddali Jun 30 '14 at 08:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56517/discussion-between-naga-mallesh-maddali-and-user2786). – Naga Mallesh Maddali Jun 30 '14 at 08:30
1

Please define this in AppDelegate.h (or) constants.h

  #define isIOS7  ([[[UIDevice currentDevice]systemVersion]floatValue] >=7.0)?YES:NO

under viewDidLoad Write this lines.

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

OR we can change it manually in the xib.

By moving each element 20px down.

Seethis image

Lova
  • 134
  • 7
0

Surprise at how few people have suggested this. Here is the correct way (no hacking)

First, make your navigationBar have a Y origin of 20.

Then in the .h file, set your ViewController as a UIBarPositioningDelegate:

@interface XYZViewController : UIViewController <UIBarPositioningDelegate>

And in the .m file:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.delegate = self;
}

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { 
    return UIBarPositionTopAttached; 
}
Jeffrey Sun
  • 7,789
  • 1
  • 24
  • 17