0

So my status bar in my app currently looks like this:

1

And I'd rather it look something like this:

2 http://mojoimage.com/free-image-hosting-12/22iOS7_StatusBar-copy.png

So that it's black with white text. At the moment it doesn't work on this particular screen because the background isn't black. And I've already set the Status Bar Style in the pList to UIStatusBarStyleLightContent.

I'll also add that the View is the initial ViewController AND it doesn't have a UINavigationController embedded into it:

EDIT: This is not a duplicate because most of the other previous solutions are outdated already.

user1416564
  • 401
  • 5
  • 18

3 Answers3

3

You can just set it to light and then toss a view behind it. You'll also have to account for device rotation if you allow that in your app.
Swift:

override func viewDidLoad() {
    super.viewDidLoad()

    let statusBG = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 21))
    statusBG.backgroundColor = UIColor.blackColor()
    view.addSubview(statusBG)
}

Objective-C:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *statusBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 21)];
    statusBG.backgroundColor = [UIColor blackColor];
    [self.view addSubview:statusBG];
}
Community
  • 1
  • 1
Chris Slowik
  • 2,859
  • 1
  • 14
  • 27
  • But this is ugly on iPhoneX. How to solve it on iPhoneX? – Gank Nov 11 '17 at 14:35
  • 1
    This is an outdated design pattern and should be avoided on iOS11. Can't fix ugly. Check out the Human Interface Guidelines from Apple for more details on iPhone X Design. https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/ – Chris Slowik Nov 26 '17 at 00:25
0

Because on iOS 6 or earlier, the status bar is not translucent, so It's better to do this based on the iOS version. In your viewDidLoad:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
    statusBarView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:statusBarView];
}

where SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

This will give you a status bar with black background and white text.

Hong Duan
  • 4,234
  • 2
  • 27
  • 50
0

in your AppDelegate applicationDidFininshLaunchingWithOptions method write below code.This helps to your all view controller.You don't need to add code in every view controller.

[UIApplication sharedApplication].statusBarHidden=NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) 
{ 
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  [application setStatusBarHidden:NO];
  self.window.clipsToBounds=YES;
  self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}
else
{
  self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  [application setStatusBarHidden:NO];
  self.window.clipsToBounds=YES;
  self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}

Then you yourprojectName.plist give

Status bar is initially hidden is NO
View controller-based status bar appearance is YES
user3182143
  • 9,459
  • 3
  • 32
  • 39