Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1338 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing the Status Bar Color for specific ViewControllers using Swift in iOS8

#1

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

Using the above code in any ViewController to set the statusBar color to White for a specific viewcontroller ***doesnt work in iOS8 for me***. Any suggestions? Using the UIApplication.sharedApplication method, the color changes after required changes in the Info.plist for the whole app.

// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent

How can I just make changes to the status bar color for some required and ***specific ViewControllers***?
Reply

#2
*(As of October 25, 2021)*

**Swift 5**, **Swift 4.2**, **Swift 4**

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
Reply

#3
*As of Oct 2020, Swift 5, Xcode 12*

If you want to set it to all view controllers in the app. and if your app has a navigation controller.

You can do it in the plist file as follow:
[![plist file changes][1]][1]

[1]:

Reply

#4
In Swift 5 or xcode 11 and later set (**View controller-based status bar appearance**) key in info.plist as NO
Then go to project target and select general, Set **status bar style** to dark or light
Reply

#5
(As of June 10, 2020)

**Swift 5** (*Without editing `.Plist` file*)

If you are using `Storyboard`, go to the `NavigationController`, select the `navigationBar`, click on the `Attributes Inspector`, then change the `style`. if you need `light content` (*white status bar*) set it anything except `default` lets say set style `black` And if you want `dark content` (*black status bar*) set it `default`.

The default (`UIBarStyleDefault`) results in the dark foreground `UIStatusBarStyleDefault` status bar. And `UIBarStyleBlack` will give a `UIStatusBarStyleLightContent` status bar.

**Programatically**



let nav = UINavigationController(rootViewController: rootViewController)

nav.navigationBar.barStyle = .default //gives you dark Content status bar

nav.navigationBar.barStyle = .black //gives you light content status bar

**Without Navigation Bar** (*Edit `.Plist`*)

add `UIViewControllerBasedStatusBarAppearance` / `View controller-based status bar appearance` to your `info.plist`, and set value is `true`.

`Override` the `preferredStatusBarStyle` property in your Controller

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

#6

Swift 4.0 Please use this code in "didFinishLaunchingWithOptions launchOptions:" Appdelegate class

UIApplication.shared.statusBarStyle = .lightContent let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView if statusBar.responds(to: #selector(setter: UIView.backgroundColor)){ statusBar.backgroundColor = UIColor.black }

iOS 13


var statusBarView: UIView = UIView()
if #available(iOS 13.0, *) {
let tag:UInt64 = 38482458385
if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(Int(tag)) {
statusBar.backgroundColor = UIColor.red
statusBarView = statusBar
} else {
let statusBar = UIView(frame: UIApplication.shared.statusBarFrame)
statusBar.tag = Int(tag)
UIApplication.shared.keyWindow?.addSubview(statusBar)
statusBarView = statusBar
}
} else {
statusBarView = (UIApplication.shared.value(forKey: "statusBar") as? UIView)!
if statusBarView.responds(to: #selector(setter: UIView.backgroundColor)){
statusBarView.backgroundColor = UIColor.red
}
}

Reply

#7
I use this way in **Swift 5, Swift 4.2**.

Add next values to Info.plist:

**UIViewControllerBasedStatusBarAppearance = YES**

or

**UIViewControllerBasedStatusBarAppearance = NO** (to see changes)


**UIStatusBarHidden = NO**

**UIStatusBarStyle = UIStatusBarStyleDefault** (or set to **UIStatusBarStyleLightContent** if you want to see *light status bar texts* on launching)


[![Info.plist][1]][1]


Then place code below to specific view controllers where you want to see light content (to see dark texts set preferredStatusBarStyle to **.darkContent**).

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

override func viewDidLoad() {
super.viewDidLoad()

if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
statusBar.backgroundColor = .sunflowerYellow
}
}


[1]:

[2]:
Reply

#8
if somebody wants to change the battery and text color of the status bar like the below image:

[![enter image description here][1]][1]

you can use the following code in the appdelegate class.

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]


[1]:
Reply

#9
There are two situation:

**1.show navigation bar**

1) add`1UIViewControllerBasedStatusBarAppearance` / `View controller-based status bar appearance` to your `info.plist`, and set value is `true`.

2) Override the preferredStatusBarStyle property in your custom NavigationController class : (from @guillama)

class NavigationController : UINavigationController {

override var preferredStatusBarStyle : UIStatusBarStyle {

if let topVC = viewControllers.last {
//return the status property of each VC, look at step 2
return topVC.preferredStatusBarStyle
}
return .default
}

3) override `preferredStatusBarStyle` in your specific view controller:

override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}

**2.hidden navigation bar**

1) ditto

2) don't need second step above, you should implement third step directly.

override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}

Reply

#10
## Custom color for the status bar (iOS11+, Swift4+)
If you are looking for a solution how to change the status bar to your custom color, this the working solution.

```swift
let statusBarView = UIView()
view.addSubview(statusBarView)
statusBarView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
statusBarView.topAnchor.constraint(equalTo: view.topAnchor),
statusBarView.leftAnchor.constraint(equalTo: view.leftAnchor),
statusBarView.rightAnchor.constraint(equalTo: view.rightAnchor),
statusBarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
statusBarView.backgroundColor = .blue
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through