94

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?

Naresh
  • 16,698
  • 6
  • 112
  • 113
Rejeesh Rajan
  • 1,019
  • 1
  • 9
  • 9
  • you can get help with this link: http://stackoverflow.com/questions/18901753/ios-7-navigation-bar-toolbar-buttons-very-close-to-status-bar/19050934#19050934 – Utkarsh Goel Oct 03 '13 at 11:54
  • [This Answer works with backgrounds apps listing as well as alert popup](https://stackoverflow.com/a/47880073/7576100) – Jack Dec 19 '17 at 04:43

25 Answers25

175

Warning: It does not work anymore with iOS 13 and Xcode 11.

========================================================================

I had to try look for other ways. Which does not involve addSubview on window. Because I am moving up the window when keyboard is presented.

Objective-C

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

Swift

func setStatusBarBackgroundColor(color: UIColor) {

    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }

    statusBar.backgroundColor = color
}

Swift 3

func setStatusBarBackgroundColor(color: UIColor) {

    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }

    statusBar.backgroundColor = color
}

Calling this form application:didFinishLaunchingWithOptions worked for me.

N.B. We have an app in the app store with this logic. So I guess it is okay with the app store policy.


Edit:

Use at your own risk. Form the commenter @Sebyddd

I had one app rejected cause of this, while another was accepted just fine. They do consider it private API usage, so you are subject to luck during the reviewing process :) – Sebyddd

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • 5
    Unlike the accepted solution, this also works when you change orientation. Thanks! – Michael Mar 06 '16 at 10:28
  • 1
    Works perfectly for me and the easiest solution. – Robert J. Clegg May 21 '16 at 10:42
  • 5
    Isn't that private API usage? – Foriger Jun 13 '16 at 12:40
  • 2
    I had one app rejected cause of this, while another was accepted just fine. They do consider it private API usage, so you are subject to luck during the reviewing process :) – Sebyddd Sep 02 '16 at 12:13
  • 1
    @Sebyddd That is bad news. I guess I was lucky all the time. Could you share us the error app store review team sent you. Did you need to deal with keyboard? What alternative approach did you take? Also I've added ur comment in the answer so people easily notice it. Thanks for sharing. – Warif Akhand Rishi Sep 04 '16 at 04:48
  • 1
    The message is pretty general and doesn't help much. I posted it bellow. In my case the keyboard was not an issue. I ended up slipping a view beneath the status bar at window level (something similar to what @shahid-iqbal did in his answer). "We found that your app uses one or more non-public APIs, which is not in compliance with the App Store Review Guidelines. The use of non-public APIs is not permissible because it can lead to a poor user experience should these APIs change." – Sebyddd Sep 04 '16 at 18:08
  • I was hoping more form apple, like.. we don't want status bar bg changed.. or if u want to change bg do this.. :-( .. anyways.. thanks for the info @Sebyddd – Warif Akhand Rishi Sep 04 '16 at 18:18
  • 1
    One of the best answers on the site, thanks @WarifAkhandRishi. I put in a long answer based on yours, which may further save people typing! – Fattie Jan 12 '17 at 20:14
  • 1
    This answer is super cool! I was searching a way to hide/modify Status Bar on simulator for iTunes Connect promo screens. With direct access to the bar I can customize it at any way. – kelin Jan 13 '17 at 11:11
  • 3
    There is an issue with this solution, when you double press the home button, this status status bar color will disappear. – Timeless Mar 01 '17 at 03:36
  • 1
    @Timeless: Status bar is hidden from the iOS if you double press. So background color disappears. – Warif Akhand Rishi Mar 01 '17 at 05:30
  • So maybe it's better to change the UI backgroundColor by the view of the status bar **position** , nor the statusBar which is UIWindow controlled by the OS when you double press the home button. Like the answer(https://stackoverflow.com/questions/47250495/a-issue-on-changing-the-color-of-the-status-bar-when-switching-apps-on-iphone), or other answer in this page. – dengApro Nov 13 '17 at 23:01
  • There is an issue with this solution, when you get a notification, status bar disappears too – Vladyslav Panchenko Apr 09 '19 at 09:35
  • 4
    Not work on iOS 13. App called -statusBar or -statusBarWindow on UIApplication: this code **must be changed** as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead. – NSDeveloper Jul 02 '19 at 07:55
111

Goto your app info.plist

  1. Set View controller-based status bar appearance to NO

  2. Set Status bar style to UIStatusBarStyleLightContent

    Then Goto your app delegate and paste the following code where you set your Windows's RootViewController.

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

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

starball
  • 20,030
  • 7
  • 43
  • 238
Shahid Iqbal
  • 2,059
  • 2
  • 21
  • 29
  • Just mentioning, apple docs recommend this if check instead: if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1){} else {} cheers! – Joel Balmer Feb 14 '14 at 17:04
  • This is ridiculously difficult to figure out, thank you for providing the elusive answer. – jpcguy89 Jun 18 '14 at 18:47
  • In the info.plist I don't see how to `Set Status bar style to UIStatusBarStyleLightContent`. Or is it that I should not expect suggestions? Also is the value of type String? – learner Aug 25 '14 at 21:22
  • Also, inside which delegate method do I place the if block? – learner Aug 25 '14 at 21:25
  • 3
    @learner Goto info.plist and then select any row. You will see a + sign. Click on Plus sign and from drop down, you should see `Status bar style` Option. Select it. And paste `UIStatusBarStyleLightContent` as its value. – Shahid Iqbal Aug 26 '14 at 21:27
  • @learner I am talking about application AppDelegate Class. Every app has an AppDelegate class. – Shahid Iqbal Aug 26 '14 at 21:28
  • @ShahidIqbal I understand that but which `method`? You simple show an if-block. In which method do I place the if-block? The delegate has a number of methods. Anyway, that was the question. But I figured it out. Thanks for replying. and +1 for the help. The method is `didFinishLaunchingWithOptions` – learner Aug 27 '14 at 20:14
  • 5
    This does not account for rotation – lostintranslation Sep 09 '14 at 18:27
  • Adding the subview was the only solution that worked for me when using a custom UINavigationBar in iOS 7 and 8. Thank you. – user3344977 Nov 13 '14 at 05:34
  • 4
    Its better to use UIScreen width: `UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];` – KlimczakM Feb 18 '15 at 11:27
  • Also consider the status bar being 40 pixels when the phone is on call or tethering modes. – Eneko Alonso May 06 '15 at 23:33
  • 2
    A more succinct way to set the frame is using `UIApplication.sharedApplication().statusBarFrame` – Doug Sep 13 '15 at 21:01
  • Make sure to call it after setting rootViewController. – Mohammad Zaid Pathan Oct 19 '16 at 06:52
  • why can't you directory change the background view color instead of creating a new view.. like `self.window?.rootViewController?.view.backgroundColor = UIColor.black` – user431791 Nov 02 '16 at 10:55
  • How to make a single view controller's status bar transparent if we use this code? – birdcage May 18 '17 at 11:42
29

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

Teja Kumar Bethina
  • 3,486
  • 26
  • 34
  • This worked well for me, but the status bar ought to be 20pt high: [[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 20)]; – LordParsley Jun 27 '16 at 09:55
28

1) set the UIViewControllerBasedStatusBarAppearance to YES in the plist

2) in viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];

3) add the following method:

 -(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

UPDATE:
also check developers-guide-to-the-ios-7-status-bar

Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
  • you can change it to black or white – Muruganandham K Sep 28 '13 at 05:45
  • 1
    This has no effect (ios7, simulator). The "preferredStatusBarStyle" is never invoked. – Adam Nov 20 '13 at 16:37
  • 1
    are you using xib?. if YES change status bar value in simulated metrics property – Muruganandham K Nov 21 '13 at 04:23
  • 3
    Ah, I found the issue. Apple's UINavigationController grabs the notification - i.e. your answer is only for when the view controller is the top controller, there are no containers (no tab-bar, no navbar, etc). – Adam Nov 21 '13 at 09:44
  • 3
    Special case when using Storyboard + NavigationController. Do #1 above. Next, create a subclass for UINavigationController (call it myNavController). In Storyboard, set the NavigationController's class to "myNavController". In myNavController.m, do #2 & #3 above. The method in #3 will now be called in your subclass (set a log or breakpoint to observe). – ObjectiveTC Feb 07 '14 at 23:51
18

You can set background color for status bar during application launch or during viewDidLoad of your view controller.

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



Here is result:

enter image description here


Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.

Here is - How to change status bar style:

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.

if you wan to set status bar style, at view controller level then follow these steps:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Will the app get rejected if we use these? Changing the statusBarView color like this is allowed? – abhimuralidharan Dec 05 '17 at 05:00
  • @abhi1992 I can't say whether apple will accept it or not because I implemented this solution in my enterprise application, which does not need to submit on App store. :) – Krunal Dec 05 '17 at 05:49
  • If I put this in a viewdidload of a viewcontroller in a tab-based app, it sets the color for every viewController, non only the one where I put the code (is this normal?) –  Dec 08 '17 at 07:56
14

In iOS 7 the status bar doesn't have a background, therefore if you put a black 20px-high view behind it you will achieve the same result as iOS 6.

Also you may want to read the iOS 7 UI Transition Guide for further information on the subject.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
8

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.

VisioN
  • 143,310
  • 32
  • 282
  • 281
sanjana
  • 203
  • 2
  • 5
7

Here's a total, copy and paste solution, with an

absolutely correct explanation

of every issue involved.

With thanks to Warif Akhand Rishi !

for the amazing find regarding keyPath statusBarWindow.statusBar. Good one.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    // handle the iOS bar!
    
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // "Status Bar Style" refers to the >>>>>color of the TEXT<<<<<< of the Apple status bar,
    // it does NOT refer to the background color of the bar. This causes a lot of confusion.
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    
    // our app is white, so we want the Apple bar to be white (with, obviously, black writing)
    
    // make the ultimate window of OUR app actually start only BELOW Apple's bar....
    // so, in storyboard, never think about the issue. design to the full height in storyboard.
    let h = UIApplication.shared.statusBarFrame.size.height
    let f = self.window?.frame
    self.window?.frame = CGRect(x: 0, y: h, width: f!.size.width, height: f!.size.height - h)
    
    // next, in your plist be sure to have this: you almost always want this anyway:
    // <key>UIViewControllerBasedStatusBarAppearance</key>
    // <false/>
    
    // next - very simply in the app Target, select "Status Bar Style" to Default.
    // Do nothing in the plist regarding "Status Bar Style" - in modern Xcode, setting
    // the "Status Bar Style" toggle simply sets the plist for you.
    
    // finally, method A:
    // set the bg of the Apple bar to white.  Technique courtesy Warif Akhand Rishi.
    // note: self.window?.clipsToBounds = true-or-false, makes no difference in method A.
    if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
        sb.backgroundColor = UIColor.white
        // if you prefer a light gray under there...
        //sb.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1)
    }
    
    /*
    // if you prefer or if necessary, method B:
    // explicitly actually add a background, in our app, to sit behind the apple bar....
    self.window?.clipsToBounds = false // MUST be false if you use this approach
    let whiteness = UIView()
    whiteness.frame = CGRect(x: 0, y: -h, width: f!.size.width, height: h)
    whiteness.backgroundColor = UIColor.green
    self.window!.addSubview(whiteness)
    */
    
    return true
}
Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
6

for the background you can easily add a view, like in example:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
[navbar addSubview:view];

where "navbar" is a UINavigationBar.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3107409
  • 61
  • 1
  • 1
  • 4
    Your first two lines are correct.. But Last Line should be [navigationController.view addSubview:view]; It should be added inside UINavigationController's view not UINavigationBar's view as it will add view after 20 px of status bar not overlapping status bar. – Shahid Iqbal Jan 10 '14 at 12:15
  • In Swift use this: let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIApplication.shared.statusBarFrame.height) let bar = UIView(frame: rect) bar.backgroundColor = UIColor.white navigationController?.view.addSubview(bar) – JVS Sep 26 '16 at 10:39
6

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];
  ...
}
bendytree
  • 13,095
  • 11
  • 75
  • 91
  • Yes this is even better to handle screen sizes and orientations. Also add something like this around this code: if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) – Benjamin Piette Oct 22 '14 at 12:14
5

Swift 4:

// Change status bar background color

let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView

statusBar?.backgroundColor = UIColor.red
Cœur
  • 37,241
  • 25
  • 195
  • 267
V.Kumar
  • 139
  • 2
  • 5
4

Change background color of status bar: Swift:

let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))    
        proxyViewForStatusBar.backgroundColor=UIColor.whiteColor()
        self.view.addSubview(proxyViewForStatusBar)
Yogesh Lolusare
  • 2,162
  • 1
  • 24
  • 35
1

In the case of swift 2.0 on iOS 9

Place the following in the app delegate, under didFinishLaunchingWithOptions:

    let view: UIView = UIView.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 20))

    view.backgroundColor = UIColor.blackColor()  //The colour you want to set

    view.alpha = 0.1   //This and the line above is set like this just if you want 
                          the status bar a darker shade of 
                          the colour you already have behind it.

    self.window!.rootViewController!.view.addSubview(view)
1

iTroid23 solution worked for me. I missed the Swift solution. So maybe this is helpful:

1) In my plist I had to add this:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

2) I didn't need to call "setNeedsStatusBarAppearanceUpdate".

3) In swift I had to add this to my UIViewController:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}
Dirk
  • 1,064
  • 1
  • 11
  • 13
1

If you're using a UINavigationController, you can use an extension like this:

extension UINavigationController {
    private struct AssociatedKeys {
        static var navigationBarBackgroundViewName = "NavigationBarBackground"
    }

    var navigationBarBackgroundView: UIView? {
        get {
            return objc_getAssociatedObject(self,
                                        &AssociatedKeys.navigationBarBackgroundViewName) as? UIView
        }
        set(newValue) {
             objc_setAssociatedObject(self,
                                 &AssociatedKeys.navigationBarBackgroundViewName,
                                 newValue,
                                 .OBJC_ASSOCIATION_RETAIN)
        }
    }

    func setNavigationBar(hidden isHidden: Bool, animated: Bool = false) {
       if animated {
           UIView.animate(withDuration: 0.3) {
               self.navigationBarBackgroundView?.isHidden = isHidden
           }
       } else {
           navigationBarBackgroundView?.isHidden = isHidden
       }
    }

    func setNavigationBarBackground(color: UIColor, includingStatusBar: Bool = true, animated: Bool = false) {
        navigationBarBackgroundView?.backgroundColor = UIColor.clear
        navigationBar.backgroundColor = UIColor.clear
        navigationBar.barTintColor = UIColor.clear

        let setupOperation = {
            if includingStatusBar {
                self.navigationBarBackgroundView?.isHidden = false
                if self.navigationBarBackgroundView == nil {
                    self.setupBackgroundView()
                }
                self.navigationBarBackgroundView?.backgroundColor = color
            } else {
                self.navigationBarBackgroundView?.isHidden = true
                self.navigationBar.backgroundColor = color
            }
        }

        if animated {
            UIView.animate(withDuration: 0.3) {
                setupOperation()
            }
        } else {
            setupOperation()
        }
    }

    private func setupBackgroundView() {
        var frame = navigationBar.frame
        frame.origin.y = 0
        frame.size.height = 64

        navigationBarBackgroundView = UIView(frame: frame)
        navigationBarBackgroundView?.translatesAutoresizingMaskIntoConstraints = true
        navigationBarBackgroundView?.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]

        navigationBarBackgroundView?.isUserInteractionEnabled = false

        view.insertSubview(navigationBarBackgroundView!, aboveSubview: navigationBar)
    }
}

It basically makes the navigation bar background transparent and uses another UIView as the background. You can call the setNavigationBarBackground method of your navigation controller to set the navigation bar background color together with the status bar.

Keep in mind that you have to then use the setNavigationBar(hidden: Bool, animated: Bool) method in the extension when you want to hide the navigation bar otherwise the view that was used as the background will still be visible.

halil_g
  • 631
  • 6
  • 11
  • For me this was the best answer since it mitigated a lot of other answer issues. The downside is the fixed frame.size.height = 64 which is wrong. One more recent way of getting the height would be -> .view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0. – Gonçalo Gaspar Jul 21 '20 at 14:21
1

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];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Karthick C
  • 1,519
  • 17
  • 16
1

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
KeyMaker00
  • 6,194
  • 2
  • 50
  • 49
S.m.g. baquer
  • 369
  • 1
  • 5
  • 9
1

The below code snippet should work with Objective C.

if (@available(iOS 13.0, *)) {
    UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
    statusBar.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
    // Fallback on earlier versions
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = [UIColor whiteColor];//set whatever color you like
    }
}
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Govind
  • 2,337
  • 33
  • 43
0

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris Alan
  • 1,426
  • 14
  • 14
0

I succeeded to customize StatusBar color pretty simple adding in AppDelegate.cs file in the method:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)

next code:

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;

if (statusBar!=null && statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
   statusBar.BackgroundColor = Color.FromHex(RedColorHex).ToUIColor();
}

So you get something like this:

enter image description here

Link: https://jorgearamirez.wordpress.com/2016/07/18/lesson-x-effects-for-the-status-bar/

Dan
  • 448
  • 10
  • 20
0

In Swift 5 and Xcode 10.2

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {

//Set status bar background colour
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
//Set navigation bar subView background colour
   for view in controller.navigationController?.navigationBar.subviews ?? [] {
      view.tintColor = UIColor.white
      view.backgroundColor = UIColor.red
   }
})

Here i fixed status bar background colour and navigation bar background colour. If you don't want navigation bar colour comment it.

Naresh
  • 16,698
  • 6
  • 112
  • 113
0

Swift code

let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: view.width, height: 20.0))
statusBarView.backgroundColor = UIColor.red
self.navigationController?.view.addSubview(statusBarView)
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Samkit Jain
  • 2,523
  • 16
  • 33
0

You can use like below, for iOS 13* and Swift 4.

1 -> Set View controller-based status bar appearance to NO

extension UIApplication {
var statusBarView: UIView? {
    if #available(iOS 13.0, *) {
       let statusBar =  UIView()

        statusBar.frame = UIApplication.shared.statusBarFrame

        UIApplication.shared.keyWindow?.addSubview(statusBar)
      
        return statusBar
    } else {
        let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
        return statusBar
    }
}

use in didFinishLaunchingWithOptions

UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
Lokesh
  • 155
  • 1
  • 11
0

Use This Extention

extension UINavigationController {

  func setStatusBar(backgroundColor: UIColor) {
    let statusBarFrame: CGRect
    if #available(iOS 13.0, *) {
        statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero
    } else {
        statusBarFrame = UIApplication.shared.statusBarFrame
    }
    let statusBarView = UIView(frame: statusBarFrame)
    statusBarView.backgroundColor = backgroundColor
    view.addSubview(statusBarView)
  }
}
Avneesh Agrawal
  • 916
  • 6
  • 11
0

Xcode 12 +

You can change it with YourProject.xcodeproj file in General Tab there is option to chnage status bar color you can set dark light or default with this option Thanks.

enter image description here

Threadripper
  • 622
  • 10
  • 15