Changing the Status Bar Color For Specific Viewcontrollers Using Swift in Ios8

Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

First Step:

Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO

Second Step (Just an explanation, no need to implement this):

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

but that affects the statusBarStyle of all the ViewControllers.

So, how to get this working for specific ViewControllers - Final Step:

Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,

Swift 2

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

Swift 3

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

This step will first change the statusBarStyle for the specific viewcontroller and then change it back to default when the specific viewcontroller disappears. Not implementing the viewWillDisappear() will change the statusBarStyle permanently to the new defined value of UIStatusBarStyle.LightContent

How to change status bar color?

Set your status bar style to dark content:

Sample Image

After that add in your info.plist View controller-based status bar appearance and set it to NO

Sample Image

UPDATE

if you want dark content only in determinate controller add setNeedsStatusBarAppearanceUpdate in viewWillAppear and after that override preferredStatusBarStyle:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return .darkContent
} else {
return .default
}

Begin with navigation Controller:

In your Scene delegate declare your first navigation controller:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let controller = UINavigationController(rootViewController: FirstViewController())
controller.navigationBar.barStyle = .black
window?.rootViewController = controller
}

in SecondViewController override the status bar style

override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return .darkContent
} else {
return .default
}
}

Change Status Bar Background Color in Swift 3

extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}

UIApplication.shared.statusBarView?.backgroundColor = .red

Update for iOS 13

App called -statusBar or -statusBarWindow on UIApplication: this code
must be changed as there's no longer a status bar or status bar
window. Use the statusBarManager object on the window scene instead.

Refer to How to change the status bar background color and text color on iOS 13?

Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

First Step:

Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO

Second Step (Just an explanation, no need to implement this):

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

but that affects the statusBarStyle of all the ViewControllers.

So, how to get this working for specific ViewControllers - Final Step:

Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,

Swift 2

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

Swift 3

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

This step will first change the statusBarStyle for the specific viewcontroller and then change it back to default when the specific viewcontroller disappears. Not implementing the viewWillDisappear() will change the statusBarStyle permanently to the new defined value of UIStatusBarStyle.LightContent

how to change statusbar color in one view controller using swift?

Set View controller-based status bar appearance in your project.plist to NO

Use viewWillAppear and will viewWillDisappear to set and reset the statusBarStyle, while keeping a property with the previous statusBarStyle like this

let initialStatusBarStyle : UIStatusBarStyle

func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
initialStatusBarStyle = UIApplication.sharedApplication().statusBarStyle
UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: animated)
}

func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().setStatusBarStyle(initialStatusBarStyle, animated: animated)
}

swift ios trying to change color of status bar on certain VC's

Try to add the following method in your VC's. Use .default or .lightContent to change the status bar color. I tested using Xcode 8 and swift 3:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.default;
}

I created a new tabbed application with Xcode 7.3.1 and swift 2.3. I have two tabs with the classes associated FirstViewController and SecondViewController. In FirstViewController I added the following method:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.Default;
}

And In the SecondViewController, I changed the background to black and I added the following method:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}

Finally, I added two buttons in the FirstViewController. One button present a controller modally and the other button present through push. When I presented the view modally the 'preferredStatusBarStyle' work but when I presented through push I need to add the following line of code:

self.navigationController?.navigationBar.barStyle = .Black

Change Status Bar Color in iOS 13?

1st way

You can check iOS version and add custom status bar like this ->

override func viewDidAppear(_ animated: Bool) {
if #available(iOS 13, *) {
let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = .systemBackground
UIApplication.shared.keyWindow?.addSubview(statusBar)
}
}

2nd way

You can customize UINavigationBarAppearance

if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = <yourColor>
navigationBar.standardAppearance = navBarAppearance
navigationBar.scrollEdgeAppearance = navBarAppearance
}

Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

First Step:

Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO

Second Step (Just an explanation, no need to implement this):

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:)
method of the AppDelegate,

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

but that affects the statusBarStyle of all the ViewControllers.

So, how to get this working for specific ViewControllers - Final Step:

Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,

Swift 2

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

Swift 3

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

This step will first change the statusBarStyle for the specific viewcontroller and then change it back to default when the specific viewcontroller disappears. Not implementing the viewWillDisappear() will change the statusBarStyle permanently to the new defined value of UIStatusBarStyle.LightContent

How to change Status Bar text color in iOS

  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())


Related Topics



Leave a reply



Submit