Swift 3
Status Bar
iOS Development
Swift Programming
Mobile App Development

How to set Status Bar Style in Swift 3

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Swift 3, customizing the status bar style is a crucial aspect of app development, allowing developers to enhance their app's user interface and readability. The status bar can be set to either light or dark content, making it versatile for different backgrounds. Below is an in-depth guide on how to manipulate the status bar style using Swift 3.

Understanding the Status Bar

The status bar is a small area at the top of the iOS device screen that displays information such as the time, battery status, and signal strength. By default, iOS provides two main styles for the status bar:

  • Light Content: Typically used for darker backgrounds.
  • Default/Dark Content: Ideal for lighter backgrounds.

Setting the Status Bar Style in Swift 3

1. Info.plist Configuration

Before Swift 3, developers managed the status bar style through the ViewController by setting the preferredStatusBarStyle property. In newer implementations like Swift 3, Apple recommends using the Info.plist configuration.

To begin:

  • Navigate to your Info.plist file.
  • Add a new key-value pair:
    • Key: View controller-based status bar appearance
    • Type: Boolean
    • Value: NO

2. Implementing preferredStatusBarStyle

Implementing preferredStatusBarStyle involves overriding a computed property in your view controller to specify the desired status bar style.

swift
1import UIKit
2
3class MyViewController: UIViewController {
4
5    override var preferredStatusBarStyle: UIStatusBarStyle {
6        return .lightContent // or .default for dark content
7    }
8    
9    override func viewDidLoad() {
10        super.viewDidLoad()
11        
12        // Additional setup
13    }
14}

3. Updating the Status Bar Style Dynamically

At times, you may need to change the status bar style dynamically based on user actions or changes within the app. For this, you should call setNeedsStatusBarAppearanceUpdate() method whenever you need to update the status bar style.

swift
1import UIKit
2
3class DynamicViewController: UIViewController {
4    
5    var isLightContent: Bool = true
6
7    override var preferredStatusBarStyle: UIStatusBarStyle {
8        return isLightContent ? .lightContent : .default
9    }
10    
11    func toggleStatusBarStyle() {
12        isLightContent = !isLightContent
13        self.setNeedsStatusBarAppearanceUpdate()
14    }
15}

Additional Topics

Managing Status Bar Visibility

Apart from the style, you might also want to show or hide the status bar dynamically:

  • Showing/Hiding the Status Bar: Override prefersStatusBarHidden property.
swift
override var prefersStatusBarHidden: Bool {
    return true // or false depending on your needs
}

Using Tab Bar and Navigation Controller

When using UITabBarController or UINavigationController, ensure that the preferredStatusBarStyle is overridden in each child UIViewController, as these controllers determine their preference based on their root controllers.

Troubleshooting Common Issues

  • Status Bar Not Updating: Ensure that View controller-based status bar appearance is set appropriately in the Info.plist.
  • Not Responding to Dynamic Changes: Double-check that setNeedsStatusBarAppearanceUpdate() is being called whenever you need to change the style dynamically.

Summary Table

Configuration StepDescription
Info.plist SettingSet View controller-based status bar appearance to NO.
Override preferredStatusBarStyleUse .lightContent or .default for preferred style.
Dynamic Status Bar UpdatesImplement setNeedsStatusBarAppearanceUpdate().
Visibility ControlManage visibility using prefersStatusBarHidden.

Adapting the status bar style in Swift 3 enhances the overall user experience by ensuring the status bar aligns with the app's visual theme. By following the steps outlined above, developers can confidently adjust the status bar to meet their design requirements.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.