How to change the status bar background color and

2019-01-01 10:36发布

My current application runs on iOS 5 and 6.

The navigation bar is having an orange color and the status bar is having a black background color with white text color. However, when I run the same application on iOS 7, I observe the status bar looks transparent with the same orange background color as the navigation bar and the status bar text color is black.

Due to this I'm not able to differentiate between the status bar and the navigation bar.

How do I make the status bar to look the same as it was in iOS 5 and 6, that is with black background color and white text color? How can I do this programmatically?

19条回答
查无此人
2楼-- · 2019-01-01 11:05

Just to add to Shahid's answer - you can account for orientation changes or different devices using this (iOS7+):

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...

  //Create the background
  UIView* statusBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 20)];
  statusBg.backgroundColor = [UIColor colorWithWhite:1 alpha:.7];

  //Add the view behind the status bar
  [self.window.rootViewController.view addSubview:statusBg];

  //set the constraints to auto-resize
  statusBg.translatesAutoresizingMaskIntoConstraints = NO;
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[statusBg(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(statusBg)]];
  [statusBg.superview setNeedsUpdateConstraints];
  ...
}
查看更多
千与千寻千般痛.
3楼-- · 2019-01-01 11:05

For bar color: You provide a custom background image for the bar.

For text color: Use the information in About Text Handling in iOS

查看更多
伤终究还是伤i
4楼-- · 2019-01-01 11:05

Please try this. Use this code in your appdelegate class "didFinishLaunchingWithOptions" function

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [application setStatusBarHidden:NO]; UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = [UIColor blackColor]; }

查看更多
回忆,回不去的记忆
5楼-- · 2019-01-01 11:08

While handling the background color of status bar in iOS 7, there are 2 cases

Case 1: View with Navigation Bar

In this case use the following code in your viewDidLoad method

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor = [UIColor yellowColor];
 [self.navigationController.navigationBar addSubview:statusBarView];

Case 2: View without Navigation Bar

In this case use the following code in your viewDidLoad method

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];

Source link http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html

查看更多
听够珍惜
6楼-- · 2019-01-01 11:08

Swift 4

In Info.plist add this property

View controller-based status bar appearance to NO

and after that in AppDelegate inside the didFinishLaunchingWithOptions add these lines of code

UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = .lightContent
查看更多
临风纵饮
7楼-- · 2019-01-01 11:10

Write this in your ViewDidLoad Method:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
}

It fixed status bar color for me and other UI misplacements also to a extent.

查看更多
登录 后发表回答