Norway


Topics

Topics

Today we’re going to take a look at how to control how the 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:

class SpaceshipViewController : UIViewController {
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }
}

The .lightContent style lives up to its name, as it makes the status bar’s content 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 actually asking the navigation controller.

We’ve all been here:

- status bars 1 - #263: Changing the Color of the Status Bar 🏴🏳 – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

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:

class LightStatusBarNavigationController : UINavigationController {
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }
}

To make this work, we can set this custom subclass in Interface Builder, or use it when creating our UI in code. Let’s have a look now:

- status bars 2 - #263: Changing the Color of the Status Bar 🏴🏳 – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

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 contained 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:

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:

UINavigationBar.appearance().barStyle = .blackOpaque

(A huge hat tip to Caleb Davenport for helping out with these !)

That takes care of the screens in our code, what about when the app first launches though?

- status bars 3 - #263: Changing the Color of the Status Bar 🏴🏳 – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

For this, we’ll need to turn to our project’s settings.

- status bars 4 - #263: Changing the Color of the Status Bar 🏴🏳 – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

Here we can hide and show the status bar during launch, and change its . Let’s take another look:

- status bars 5 - #263: Changing the Color of the Status Bar 🏴🏳 – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

Perfect!



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here