Navigation bar and Status bar issue in iOS 7

柔情痞子 提交于 2019-12-23 05:41:42

问题


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 :

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 :

Any help will be very appreciated, Thanks.

Third screen :


回答1:


Add following code in your viewDidLoad method :

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

if (systemVersion >= 7.0) 
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
}



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/24484352/navigation-bar-and-status-bar-issue-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!