---
title: "#263: Changing the Color of the Status Bar 🏴🏳"
url: https://littlebitesofcocoa.com/263-changing-the-color-of-the-status-bar
author: Jake Marsh
date: 2016-09-13
published: 2016-09-13
topics: ["UIKit"]
description: "Today we're going to take a look at how to control how the status bar appears in our app. Let's get started. The simplest way to change how the status bar..."
---

# #263: Changing the Color of the Status Bar 🏴🏳

By Jake Marsh, September 13, 2016. HTML: https://littlebitesofcocoa.com/263-changing-the-color-of-the-status-bar

Today we're going to take a look at how to control how the _status bar_ appears in our **app**. Let's get started.

The simplest way to change how the **status bar** looks in our **app** is to override `preferredStatusBarStyle`:

``` swift
class SpaceshipViewController : UIViewController {
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }
}
```

The `.lightContent` style lives up to its name, as it makes the **status bar's** <i>content</i> white.

This work great until we put our **view controller** inside a **navigation controller**. Overriding the **property** then has no effect, as the system is <i>actually</i> asking the **navigation controller**.

We've all been here:

<img src="https://media.littlebitesofcocoa.com/status-bars-1.png" style="max-width: 375px" />

Yuck. Let's fix that up.

Since our content here is in a **navigation controller** we can use a "tried and true" approach that involves simply subclassing `UINavigationController`:

``` swift
class LightStatusBarNavigationController : UINavigationController {
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }
}
```

To make this work, we can set this custom **subclass** in **Interface Builder**, or use <i>it</i> when creating our UI in code. Let's have a look now:

<img src="https://media.littlebitesofcocoa.com/status-bars-2.png" style="max-width: 375px" />

Nice.

This approach works in our case, but might not be the best way to do this. If the **navigation bar** ever gets hidden, **UIKit** will actually start asking the <i>contained</i> **view controller** for the **status bar's style**, which may or may not be what we want.

We could have accomplished the same thing using a plain old `UINavigationController` by simply setting its `barStyle` property when we create it (or by setting this property in Interface Builder) to the `.blackOpaque` style:

``` swift
let nc = UINavigationController(rootViewController: spaceshipsVC)
navigationController?.navigationBar.barStyle = .blackOpaque
```

Or, even better, we could set this style using `UINavigationBar`'s **appearance proxy**, so this style is applied to all the **navigation bars** in our **app**:

``` swift
UINavigationBar.appearance().barStyle = .blackOpaque
```

(A huge hat tip to [Caleb Davenport](calebd) for helping out with these techniques!)

That takes care of the screens in our code, what about when the app first launches though? 

<img src="https://media.littlebitesofcocoa.com/status-bars-3.png" style="max-width: 375px" />

For this, we'll need to turn to our **project's settings**.

<img src="https://media.littlebitesofcocoa.com/status-bars-4.png" style="max-width: 420px" />

Here we can hide and show the **status bar** during launch, and change its color. Let's take another look:

<img src="https://media.littlebitesofcocoa.com/status-bars-5.png" style="max-width: 375px" />

Perfect!

<p></p>
