7

How to change Status Bar text color in iOS

 1 year ago
source link: https://sarunw.com/posts/change-status-bar-text-color-in-uikit/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

By default, the color of a status bar text will change according to the device appearance setting. For example, the text will be black on light appearance and white text on dark appearance.

A status bar color can be white or black.

A status bar color can be white or black.

There are two ways to opt-out of this behavior and set the status bar text color yourselves.

Change status bar text color for the Entire App using Info.plist

If you want to set the status bar text color for an entire app, you can do it by specifying two keys in Info.plist.

  1. "Status bar style" (UIStatusBarStyle)
  2. "View controller-based status bar appearance" (UIViewControllerBasedStatusBarAppearance)

Here are the steps to do it.

  1. Open your Info.plist, either from the Project Navigation or the "Info" tab.
Info.plist

Info.plist

  1. Add key "Status bar style" (UIStatusBarStyle) and set value to either "Light Content" (UIStatusBarStyleLightContent) or "Dark Content" (UIStatusBarStyleDarkContent).
  2. Add key "View controller-based status bar appearance" (UIViewControllerBasedStatusBarAppearance) and set its value to "NO".
Add two keys to Info.plist.

Add two keys to Info.plist.

Change status bar text color for specific view controller

By default, the status bar text color will change according to active view controller.

A view controller communicates the preferred text color to the system via preferredStatusBarStyle property.

You can control the status bar text color by override preferredStatusBarStyle property in a view controller and return the style you want.

In this example, we set the status bar text color to white.

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

Make sure you set "View controller-based status bar appearance" (UIViewControllerBasedStatusBarAppearance) key in Info.plist to YES.

Or you can simply remove the key. The default behavior is already using a view controller-based status bar.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK