Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Status Bar text color in iOS

My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?

like image 633
Oleksandr Veremchuk Avatar asked Jul 16 '13 14:07

Oleksandr Veremchuk


People also ask

How do I change the color of my status bar on my Iphone?

Go to the Storyboard. Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content.

How do I change the color of my status bar text?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.

How do I change the color of my status bar in Swift?

Open your info. plist and set UIViewControllerBasedStatusBarAppearance to false . In the first function in AppDelegate. swift , which contains didFinishLaunchingWithOptions , set the color you want.


1 Answers

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file.

  2. In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];

  3. Add the following method:

    - (UIStatusBarStyle)preferredStatusBarStyle {      return UIStatusBarStyleLightContent;  } 

Note: This does not work for controllers inside UINavigationController, please see Tyson's comment below :)

Swift 3 - This will work controllers inside UINavigationController. Add this code inside your controller.

// Preferred status bar style lightContent to use on dark background. // Swift 3 override var preferredStatusBarStyle: UIStatusBarStyle {     return .lightContent } 

Swift 5 and SwiftUI

For SwiftUI create a new swift file called HostingController.swift

import Foundation import UIKit import SwiftUI  class HostingController: UIHostingController<ContentView> {     override var preferredStatusBarStyle: UIStatusBarStyle {         return .lightContent     } } 

Then change the following lines of code in the SceneDelegate.swift

window.rootViewController = UIHostingController(rootView: ContentView()) 

to

window.rootViewController = HostingController(rootView: ContentView()) 
like image 193
Peter B. Kramer Avatar answered Oct 12 '22 08:10

Peter B. Kramer